繁体   English   中英

需要有关mysql错误的帮助

[英]Need assistance with a mysql error

希望有人可以帮助我解决问题。 它似乎是一个语法问题,但我无法确定问题到底是什么...我正在使用最新版本的mysql

我收到此错误:

“警告:mysql_fetch_assoc()期望参数1是资源,在第14行的C:... \\ testing_album_func.php中给出布尔值”

这是我的代码:

function get_listings() { 

    $listings = array();

    $listings_query = mysql_query("SELECT `class_ads`.`ad_id`, `class_ads`.`created_on`, 
                LEFT(`class_ads`.`title`, 50) as `title`, LEFT(`class_ads`.`description`, 50) as  
                `description`, `class_ads`.`price`, `class_ads`.`quantity`,   
                COUNT(`images`.`image_id`) as `image_count` FROM `class_ads` LEFT JOIN `images` ON 
                `class_ads`.`ad_id`=`images`.`ad_id` WHERE `user_id`='".$_SESSION['user_id']."' GROUP
                 BY `class_ads`.`ad_id`");


    while ($listings_row= mysql_fetch_assoc($listings_query)) {

        $listings[] = array(
        'ad_id' => $listings_row['ad_id'],
        'created_on' => $listings_row['created_on'],
        'title' => $listings_row['title'],
        'description' => $listings_row['description'],
        'price' => $listings_row['price'],
        'quantity' => $listings_row['quantity'],
        'image_count' => $listings_row['image_count']   
        );

    }

    return $listings;
}

如果有错误, mysql_query返回false。

您应该将支票如下所示:

if(!$listings_query) {
     die('An error occurred.  The message is: ' . mysql_error());
}

您的SQL语句看起来有些时髦,因此您还应该打印出实际的查询并确保它看起来不错。

你应该为起动器,添加一个错误您的查询报表,所以你知道到底发生了什么错

$listing_query = mysql_query(".....") or die (mysql_error());

其次 ,您要在函数内部调用查询,这可能是因为没有传递链接标识符,而是在函数范围内。 尝试在函数内连接和选择数据库,或者将链接资源作为参数传递给函数,或者使其具有全局性(不要这样做)。

第三 ,您可以尝试在phpmyadmin之类的工具或命令行中运行查询,以便立即查看查询是否失败以及原因; 我通常在将复杂查询放入php脚本之前执行此操作。

另外 ,错误可能是WHERE user_id='".$_SESSION['user_id']."'但我不确定(您不需要告诉此列所在的表吗?)。 参见上面的三句话,您将自己发现;)

在数据库中执行查询时发生错误。 您应该查看您尝试运行的SQL,可能是名称不正确的字段。 要获取错误的更多详细信息,请使用mysql_error函数。

echo mysql_error($listings_query);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM