-   Programming (
-   -   mysql query variable (

Zero4 08-25-2011 09:50 PM

mysql query variable
I have a mysql query with a variable within it that wont work, here it is

PHP Code:

("SELECT `image_id`,`album_id`,`timestamp`,`ext` FROM `images` WHERE `album_id = $album_id"

The problem is that the query does not recongise the variable $album_id
Can anyone help with my problem

Thanks in advance

Zero4 08-25-2011 09:52 PM

There is a back tick at the end of album_id after WHERE did'nt show up

graemef 08-26-2011 10:35 PM

I guess you need to provide a little more information.

What language are you using (SQL on it's own will not support variables, although binding come close)

Maybe provide some of the lines around your statement including where the variable is declared.

Zero4 08-27-2011 09:16 PM

re mysql variable
I am using php
here's the code

PHP Code:

include 'init.php';
<h3> View Album</h3>;

$images get_images($album_id);

$images)) {
'No images in this Album';
include 'template/footer.php';

Here's the function

PHP Code:

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");
while (
$images_row mysql_fetch_assoc($image_query)) {
$image[] = array (
'id' => $images_row['image_id'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext'],

graemef 08-27-2011 09:33 PM

In the function inside the while loop you use an array called $image, but you are returning an array called $images

If that doesn't fix it I'd suggest that you add some print statements displaying the values of your query and other useful variables as you are executing the code.

Zero4 08-28-2011 09:50 PM

Hi graemef

That was a typo sorry. Howerver the query does not work

SELECT `image_id`,`album_id`,`timestamp`,`ext` FROM images WHERE `album_id` = $album_id"

sql returns this message
#1054 Unknown column '$album_id' in where clause

Thank you for your reply

Wim Sturkenboom 08-28-2011 10:01 PM

Place $album_id between single quotes.


SELECT `image_id`,`album_id`,`timestamp`,`ext` FROM images WHERE `album_id` = '$album_id'
The above might not straight away work (not sure if the $ will be taken literally in php)


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

All times are GMT -5. The time now is 12:52 AM.