我收到以下错误:
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)
由于
答案 0 :(得分:0)
var_dump($query);
echo mysql_error();
答案 1 :(得分:0)
mysql_query是否失败并且return FALSE?正如文档所述:
用于SELECT,SHOW,DESCRIBE,EXPLAIN 和其他声明返回 resultset,mysql_query()返回一个 成功的资源,或FALSE 错误。
FALSE不是有效的资源。
答案 2 :(得分:0)
在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