I earlier made a database in mysql and now i am trying to list all the values from it in a table, but I get the following error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean
on line: while ($row=mysql_fetch_array($result))
Here is my code:
$con=mysql_connect("localhost","root","");
if (!$con) {
die("Error: " . mysql_error);
}
mysql_select_db("my_db",$con);
$result = mysql_query("SELECT * FROM Users");
echo "<table border='1'>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Email adress</th>
</tr>";
while($row=mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['emailadress'] . "</td>";
echo"</tr>";
}
echo "</table>";
mysql_close($con);
I read other similar question but diden't get an answer.
Your code doesn't evaluate the response from MySQL about selecting the database or running your query. The error indicates that your query didn't succeed (hence mysql_query returns FALSE) - which means one or both of the above didn't work.
Test for errors when you select the database and use die(mysql_error()); to see why these calls are failing.
Make sure this line is actually getting the rows from the table: $result = mysql_query("SELECT * FROM Users");
Try doing an error check in that query:
$result = mysql_query("SELECT * FROM Users") or die(mysql_error());
Possibly your table is called "users" and not "Users" (note the caps), so change accordingly.
Regards, Richi