执行页面时出现此错误:
警告:
mysql_fetch_assoc()
:提供的参数不是有效的MySQL结果资源...
我尝试在phpMyAdmin上运行SQL目录,运行正常。
以下是完整代码:
<?php
$connect_error = 'Sorry, we have connection problems.';
mysql_connect('localhost','user','password') or die($connect_error);
mysql_select_db('mydb') or die($connect_error);
$result = mysql_query("SELECT * FROM tbl_main ORDER BY id desc limit 1");
$rows = array();
while($r = mysql_fetch_assoc($result)) { //ERROR POINTS HERE
$rows['id'][] = $r;
}
print json_encode($rows);
?>
为什么我收到此错误?
答案 0 :(得分:1)
如果mysql_query
返回FALSE,那么您将收到错误消息。如果您的SQL出现问题,或者数据库连接出现问题,它可能会返回false。
调用mysql_error()以了解有关发生的错误的更多信息。
<?php
$connect_error = 'Sorry, we have connection problems.';
$link = mysql_connect('localhost','user','password') or die($connect_error);
mysql_select_db('mydb', $link) or die($connect_error);
$result = mysql_query("SELECT * FROM tbl_main ORDER BY id desc limit 1", $link);
if ($result) {
$rows = array();
while($r = mysql_fetch_assoc($result)) { //ERROR POINTS HERE
$rows['id'][] = $r;
}
print json_encode($rows);
} else {
print mysql_error($link);
}
?>