Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
08-25-2011, 09:50 PM
#1
Member
Registered: Jun 2007
Posts: 41
Rep:
mysql query variable
Hi
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
08-25-2011, 09:52 PM
#2
Member
Registered: Jun 2007
Posts: 41
Original Poster
Rep:
There is a back tick at the end of album_id after WHERE did'nt show up
08-26-2011, 10:35 PM
#3
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352
Rep:
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.
08-27-2011, 09:16 PM
#4
Member
Registered: Jun 2007
Posts: 41
Original Poster
Rep:
re mysql variable
I am using php
here's the code
PHP Code:
<?php
include 'init.php' ;
include 'template/header.php' ;
?>
<h3> View Album</h3>;
<?php
$album_id = $_GET [ 'album_id' ];
$images = get_images ( $album_id );
if(empty( $images )) {
echo 'No images in this Album' ;
}else{
print_r ( $images );
}
?>
<?php
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' ],
);
}
return $images ;
}
08-27-2011, 09:33 PM
#5
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352
Rep:
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.
08-28-2011, 09:50 PM
#6
Member
Registered: Jun 2007
Posts: 41
Original Poster
Rep:
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
08-28-2011, 10:01 PM
#7
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325
Rep:
Place $album_id between single quotes.
Code:
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)
Code:
$image_query = mysql_query("SELECT `image_id`,`album_id`,`timestamp`,`ext` FROM images WHERE `album_id` = '" . $album_id . "'");
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 09:03 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News