LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 08-25-2011, 10:50 PM   #1
Zero4
Member
 
Registered: Jun 2007
Posts: 53

Rep: Reputation: 0
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
 
Old 08-25-2011, 10:52 PM   #2
Zero4
Member
 
Registered: Jun 2007
Posts: 53

Original Poster
Rep: Reputation: 0
There is a back tick at the end of album_id after WHERE did'nt show up
 
Old 08-26-2011, 11:35 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 08-27-2011, 10:16 PM   #4
Zero4
Member
 
Registered: Jun 2007
Posts: 53

Original Poster
Rep: Reputation: 0
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;     
    

 
Old 08-27-2011, 10:33 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 08-28-2011, 10:50 PM   #6
Zero4
Member
 
Registered: Jun 2007
Posts: 53

Original Poster
Rep: Reputation: 0
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
 
Old 08-28-2011, 11:01 PM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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 . "'");
 
  


Reply

Tags
mysql


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a variable from shell script to mysql query? ne00 Programming 1 06-10-2009 12:11 PM
Using Variable in Linux Bash for MySQL Query ThaSaltyDawg Linux - Newbie 6 11-05-2008 01:03 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM
php mysql variable arguments in query. ldp Linux - Software 0 10-26-2004 08:56 AM
How to pass mysql query to a variable? chynna_v Programming 4 09-03-2004 06:09 AM


All times are GMT -5. The time now is 04:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration