0

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

You all are probably getting tired of me posting meaningless stuff but I have yet again one more dumb issue..I am getting an undefined index error when I shouldn't be. The error says this:

"Notice: Undefined index: album_id in C:\Program Files (x86)\EasyPHP-5.3.9\www\Image Upload\func\image.func.php on line 24"

Here is the where the issue is in the file:

function get_images($album_id){
$album_id = (int)$album_id;

$images = array();

$image_query = mysql_query("SELECT `image_id`, `album_id` `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']);
while($images_row = mysql_fetch_assoc($image_query)){
    $images[] = array(
        'id' => $images_row['image_id'],
        'album' => $images_row['album_id'], //<<<<Here is error
        'timestamp' => $images_row['timestamp'],
        'ext' => $images_row['ext']
    );
}
return $images;
}

Thanks again for all of your patience!

-TechGuy24

Community
  • 1
  • 1
WebDeVGuy24
  • 39
  • 1
  • 9
  • 3
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799). Instead you should learn about prepared statements and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you care to learn, [here is a quite good PDO-related tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – DCoder Sep 01 '12 at 06:49

1 Answers1

2

There is a syntax error in your query

"SELECT `image_id`, `album_id` `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']

=============================== ^

a comma is missing between album_id and timestamp

Change your query to

"SELECT `image_id`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id AND `user_id`=".$_SESSION['user_id']

and it will work like a charm :)

ALWAYS ALWAYS ALWAYS, first run queries on your database, before firing them with php. This will help you find errors faster. Of course you can see what error was there using php too, but why waste time?

Use phpMyAdmin to run queries. :)

PS : He who asks appears fool for 5 mins, he who doesn't ask remains fool forever. You have a question(however dumb it may be)? POST IT.

AdityaParab
  • 7,024
  • 3
  • 27
  • 40
  • I can't believe I missed that...dumb on my part, my appoligies for wasting your time :P – WebDeVGuy24 Sep 01 '12 at 06:56
  • lol.. its alright.. :) Why so self hatred? – AdityaParab Sep 01 '12 at 06:58
  • Get into the habit of using `print_r` or `var_dump` to see the contents of arrays when you run into problems like that. You would then have noticed an element with the key 'timestamp' where 'album_id' should have been. – Barmar Sep 01 '12 at 06:59
  • @DCoder - I will definitely look into what you sent me! Thanks for informing me! – WebDeVGuy24 Sep 01 '12 at 06:59
  • @Maverick - there's no self hatred I just don't like posting things that seem so easy to figure out but yet seems impossible when I look at the code :P – WebDeVGuy24 Sep 01 '12 at 07:01