我当前收到错误:
警告:mysql_fetch_assoc((要求参数1为resource,在第23行C:''examplep''htdocs''Project''core''functions''image.php中给定布尔值
我尝试过mysql_error,它返回:
查询为空
我看过这个查询,似乎没有什么问题,我不明白我做错了什么。
这是代码:
function get_images($album_id) {
$album_id = (int)$album_id;
$images = array();
$image_query = mysql_query("SELECT `image_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND `customers_custnum`=".$_SESSION["customers_custnum"]);
while ($images_row = mysql_fetch_assoc($image_query)) {
$images[] = array(
'id' => $images_row["image_id"],
'album' => $images_row["album_id"],
'timestamp' => $images_row["timestamp"],
'ext' => $images_row["ext"]
);
}
return $images;
}
如有任何帮助,我们将不胜感激!谢谢你的光临。
尝试添加一些错误处理,这样您就可以看到错误和尝试执行的SQL,例如
$sql="SELECT `image_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND `customers_custnum`=".$_SESSION["customers_custnum"];
$image_query = mysql_query($sql);
if ($image_query){
while ($images_row = mysql_fetch_assoc($image_query)) {
$images[] = array(
'id' => $images_row["image_id"],
'album' => $images_row["album_id"],
'timestamp' => $images_row["timestamp"],
'ext' => $images_row["ext"]
);
}
return $images;
}
else
{
$err=mysql_error();
print "FAILED: $err while executing $sql";
}
首先,您不应该使用mysql_*
函数。看到这里的红色大盒子。请考虑改用PDO或MySQLi。
其次,看起来您可能对SQL注入持开放态度。你应该回避你的查询。
第三,在进行过程中,您应该始终进行错误检查。根据mysql_fetch_assoc((的文档:
返回与已提取行,如果没有更多行,则返回FALSE。
所以你应该做一些类似的事情:
$res = mysql_fetch_assoc($query);
if(!$res) {
die('Query error: ' . mysql_error());
}
尽管如此,你可能想变得不那么残忍,而不是简单地死去。
您的查询有问题。这就是mysql_query返回FALSE而不是结果集或"resource">的原因。
您的查询是错误的。正确的查询是:
$image_query = mysql_query("SELECT `image_id`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND `customers_custnum`=".$_SESSION['customers_custnum']);
查询字符串中缺少album_id
。Edit
您的查询和代码将使用perfectly
。