Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

php - Mysql: Warning: mysql_fetch_array() expects parameter 1 to be resource

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...