0

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();

    $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;
}
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • You are not testing your query for errors. See the PHP manual on mysql_query(), or [Reference: What is a perfect code sample using the mysql extension?](http://stackoverflow.com/q/6198104) – Pekka Oct 03 '11 at 22:04
  • I think problem is your SQL statement. SQL isn't returning as a resource. Because of this you are getting this error. Can you try more basic statement instead of this, for testing ? For example : `SELECT ad_id FROM class_ads` – Eray Oct 03 '11 at 22:18
  • 1
    Why do so many people write mysql_query() queries without error checking? Is there a broken tutorial somewhere that shows it this way? – Pekka Oct 03 '11 at 22:21
  • 1
    @Pekka -- the internet is a giant broken php/mysql tutorial. That's why we're all here at Stack Overflow, fixing it. – Jonathan Beebe Oct 03 '11 at 22:24
  • @something hahahaha! Hard to deny. – Pekka Oct 03 '11 at 22:25
  • possible duplicate of [Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error](http://stackoverflow.com/questions/11674312/warning-mysql-fetch-expects-parameter-1-to-be-resource-boolean-given-error) – John Conde Jul 28 '12 at 16:41

3 Answers3

1

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.

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • Thank you, I added the error reporting and it came back with: "An error occurred. The message is: Column 'user_id' in where clause is ambiguous". I was able to fix it in two seconds. THANKS STACKOVERFLOW, YOU'RE THE GREATEST! lol. Seriously though, thanks, I just couldn't figure it out. – system_se7en Oct 04 '11 at 13:26
1

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 ;)

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • You were right about where the error was. I didn't think I needed to specify since the field `user_id` resides on both tables... but of course I did. Thanks again. – system_se7en Oct 04 '11 at 13:30
1

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);
Jaison Erick
  • 655
  • 5
  • 11