-1

I am currently getting the error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Project\core\functions\image.php on line 23

I have tried the mysql_error and it returned:

Query is Empty

I have looked at the query and there seems to be nothing wrong with it, I don't understand what I have done wrong.

Here is the code:

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

Any help would be greatly appreciated! Thank you for looking.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • you have error in your query, or not connected to database, try to echo query and execute it using phpMyAdmin –  Mar 24 '13 at 17:33
  • 1
    [Please, don't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [pdo](https://wiki.php.net/rfc/mysql_deprecation) or [mysqli](http://stackoverflow.com/questions/tagged/mysqli). – zessx Mar 24 '13 at 17:38
  • I have found the problem at last. Turns out I am just an idiot and wrongly named the database column, I named it user_id instead of customers_custnum. <_> Thank you all for your time, I really appreciate it! :) – Auggers Mar 24 '13 at 17:51

4 Answers4

1

Try adding some error handling so that you can see both the error and the SQL you tried to execute, e.g. something like

$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";
}
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
1

Firstly, you should not be using mysql_* functions. See the big red box here. Consider using PDO or MySQLi instead.

Secondly, it looks like you may be leaving yourself open to SQL injection. You should be escaping your queries.

Thirdly, you should always be error checking as you go along. As per the docs for mysql_fetch_assoc():

Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

So you should be doing something similar to:

$res = mysql_fetch_assoc($query);
if(!$res) {
   die('Query error: ' . mysql_error());
}

Although, you may want to be a little less brutal than simply dying out.

juco
  • 6,331
  • 3
  • 25
  • 42
  • Thank you for your advice, I am relatively new to PHP and learning from old Youtube Videos, which would explain the old version of PHP. – Auggers Mar 24 '13 at 18:05
  • It would definitely be worth checking the documentation on [php.net](http://php.net) before using any functions from the videos. tutsplus have also done a nice [tutorial on PDO](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) - It may also be worth running through that – juco Mar 24 '13 at 18:10
0

there is something wrong with your query. that is why mysql_query is returning a FALSE instead of a resultset or "resource".

Mayukh Roy
  • 1,815
  • 3
  • 19
  • 31
0

Your query is wrong. The correct query is:

$image_query = mysql_query("SELECT `image_id`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND  `customers_custnum`=".$_SESSION['customers_custnum']);

You were missing album_id from query string. Edit your query and your code will work perfectly.

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71