【发布时间】:2013-09-01 22:06:30
【问题描述】:
我收到错误“mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源...”。在与此 mysql 查询有关的行上。
$result = mysql_query("select * from chat order by time desc limit 0,10");
$messages = array();
while($row = mysql_fetch_array($result)){
$messages[] = "<div class='message'><div class='messagehead'>" . $row[name] . " - " . date('g:i A M, d Y',$row[time]) . "</div><div class='messagecontent'>" . $row[message] . "</div></div>";
//The last posts date
$old = $row[time];
}
//Display the messages in an ascending order, so the newest message will be at the bottom
for($i=9;$i>=0;$i--){
echo $messages[$i];
}
【问题讨论】:
-
我想您的查询失败了;但是您没有对此进行检查,只是假设它有效。您应该检查来自
mysql_query的退货并妥善处理;您可以在mysql_error中找到问题的全部详细信息。 -
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果选择 PDO,here is a good tutorial -
这里有没有做过基本的调试?使用
mysql_error函数来查看您的查询有什么问题。如果您搜索网站中已回答的数百万个重复项,也会有所帮助。 -
你的下一个通知将是:使用未定义的常量名称 - 假定为 'name' 和 ...