[英]php/sql error - mysql_fetch_assoc()
我收到以下错误:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource..
我不知道发生了什么。 这是我的代码。 我确实使用名为notes的表创建了一个数据库。 此外,数据库连接正常工作。 有什么想法吗?
$query = mysql_query("SELECT * FROM notes ORDER BY id DESC");
mysql_fetch_assoc($query)
谢谢
var_dump($query);
echo mysql_error();
mysql_query失败并返回FALSE吗? 如文档所述:
对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()成功时返回资源,错误时返回FALSE。
FALSE将不是有效的资源。
在PHP页面上,它为您提供了如何使用它以及进行错误检查的完美示例。
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
您如何执行整个方法以确保返回行及其有效资源,并且结果是否正确?
您应该始终进行调试,并且永远不要假设正确执行了某些事情。
以后请仔细阅读php.net上的函数的整个页面,在这种情况下,它是http://php.net/manual/en/function.mysql-fetch-assoc.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.