-2
<?php
$sql = mysql_query("SELECT * FROM users;");

    while($row = mysql_fetch_array($sql))
    {
    $dbCon=mysqli_connect("localhost", "root", "", "dbusers")
       or die(mysqli_error()."Connection disconnected");
    echo "<tr>";
        echo "<td>" . $row['UserID'] . "</td>";
        echo "<td>" . $row['Firstname'] . "</td>";
        echo "<td>" . $row['Lastname'] . "</td>";
        echo "<td>" . $row['Gender'] . "</td>";
        echo "<td>" . $row['Email'] . "</td>";
        echo "<td>" . $row['Status'] . "</td>";
        echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
    }

?>
  • What's wrong with my code?

Here's the error i always get: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\CRUD\CRUD_Act\includes\dbdisp.php on line 4

Colin 't Hart
  • 6,771
  • 2
  • 27
  • 48

3 Answers3

1

connect to database first!

<?php
 $dbCon=mysql_connect("localhost", "root", "", "dbusers")
       or die(mysql_error()."Connection disconnected");
$sql = mysql_query("SELECT * FROM users;");

    while($row = mysql_fetch_array($sql))
    {

    echo "<tr>";
        echo "<td>" . $row['UserID'] . "</td>";
        echo "<td>" . $row['Firstname'] . "</td>";
        echo "<td>" . $row['Lastname'] . "</td>";
        echo "<td>" . $row['Gender'] . "</td>";
        echo "<td>" . $row['Email'] . "</td>";
        echo "<td>" . $row['Status'] . "</td>";
        echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
    }

?>
Marcel Korpel
  • 21,285
  • 5
  • 59
  • 80
Ali Akbar Azizi
  • 2,946
  • 2
  • 23
  • 40
  • I already did this but the same error was given to me: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\CRUD\CRUD_Act\displayRecords.php on line 54 – Jullien Nikka Olay Mar 29 '13 at 02:58
  • @Jullien Nikka Olay are you sure you have data in `users` ( not user ) table? – Ali Akbar Azizi Mar 29 '13 at 11:33
0

You are querying the mysql database before you connect to it. Before you do ANY database work you have to connect to it then check if you have a connection. The Error returns false because it could not run the query as there was no database to query.

<?php
$dbCon=mysql_connect("localhost", "root", "", "dbusers")
   or die(mysql_error()."Connection disconnected");
$sql = mysql_query("SELECT * FROM users;");

while($row = mysql_fetch_array($sql))
{
    echo "<tr>";
    echo "<td>" . $row['UserID'] . "</td>";
    echo "<td>" . $row['Firstname'] . "</td>";
    echo "<td>" . $row['Lastname'] . "</td>";
    echo "<td>" . $row['Gender'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "<td>" . $row['Status'] . "</td>";
    echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
}

This should be just under php tag

NoLiver92
  • 880
  • 2
  • 15
  • 38
0

As they all say : you must be connected to data base

Include your file connection :

like :

include("connexion.php");

or do like they suggest, because you querying the mysql database before you connect to it.

and then :

  <?php
   $sql ="SELECT * FROM users";
   $res=mysql_query($sql) or die("erreur");
    while($row=mysql_fetch_row($res))
    {
    echo "<tr>";
        echo "<td>" . $row['UserID'] . "</td>";
        echo "<td>" . $row['Firstname'] . "</td>";
        echo "<td>" . $row['Lastname'] . "</td>";
        echo "<td>" . $row['Gender'] . "</td>";
        echo "<td>" . $row['Email'] . "</td>";
        echo "<td>" . $row['Status'] . "</td>";
        echo "<td>" . $row['Date_joined'] . "</td>";
    echo "</tr>";
    }

?>

PS : The query string should not end with a semicolon

Zero-dev
  • 220
  • 3
  • 14
  • @Marcel Korpel , Code updated, thanks – Zero-dev Mar 28 '13 at 12:56
  • I already did this but the same error is given: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\CRUD\CRUD_Act\displayRecords.php on line 54 – Jullien Nikka Olay Mar 29 '13 at 02:59
  • To reveal the errors, try this: `$res= mysql_query($res) or die("Error : ".mysql_error());` and Post the error here – Zero-dev Mar 29 '13 at 08:52