开发者

Need assistance with a mysql error

Hoping someone can help me out with a problem. it appears to be a syntax problem but I can't hammer down what exactly the problem is... I am using the latest version of mysql

I am getting this error:

"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:...\testing_album_func.php on line 14"

Here is my code:

function get_listings() { 

    $listings = array(开发者_Go百科);

    $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 returns false if there is an error.

You should put a check like:

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

Your SQL statement looks a bit funky, so you should also print out the actual query and make sure it looks fine.


An error occurred while executing the query in the database. You should review the SQL that you is trying to run, probably a field with the incorrect name. To get more details of the error, use the mysql_error function.

echo mysql_error($listings_query);


You should, for starter, add an error reporting to your query, so you know exactly what's going wrong

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

Second, you're calling the query inside a function, it might be that the link identifier is not being passed, being inside the scope of the function. Try connecting and selecting your database WITHIN the function, or passing the link resource as an argument to the function, or having it global (don't do this).

Third, you could try running your query in a tool like phpmyadmin or a command line, so that you see at once if the query fails and why; I usually do this with complex queries before putting them into a php script.

Also, error might be WHERE user_id='".$_SESSION['user_id']."' but I'm not sure about it (don't you need to tell the table this column resides in?). See the three remarks above and you'll find out by yourself ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜