Simple Query Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Simple Query Question

Post by daebat »

Code: Select all

$data = mysql_query("SELECT `id` FROM `productimages` INNER JOIN `pimagecategories` ON `productimages`.`category` = `categories`.`id` WHERE `categories`.`id` = 1;") or die(mysql_error());
returns
Column 'id' in field list is ambiguous
I know that this means that I'm using ID twice but what do I do to make it work?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Simple Query Question

Post by Mark Baker »

Code: Select all

$data = mysql_query("SELECT `productimages`.`id` FROM `productimages` INNER JOIN `pimagecategories` ON `productimages`.`category` = `categories`.`id` WHERE `categories`.`id` = 1;") or die(mysql_error());
And you only need the `backticks` when the table or column name is a mysql reserved word
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Simple Query Question

Post by jackpf »

Prefix it with the table name :)

Doh, didn't see your post mark :P
Yeah. I think it's good practice to use backticks on all table/field names anyway...but it's just preference i guess.
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Re: Simple Query Question

Post by daebat »

THANKS!@ :P
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Simple Query Question

Post by jackpf »

Don't you mean
[sql]SELECT `productimages`.`id`FROM `productimages`, `categories`INNER JOIN `pimagecategories` ON `productimages`.`category` = `categories`.`id`WHERE `categories`.`id` = 1;[/sql]?

Although I obviously can't be sure,since you haven't posted your database structure... :)
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Re: Simple Query Question

Post by daebat »

Ok, now I am not echoing any of the data:

Code: Select all

<?php
$data = mysql_query("SELECT `productimages`.`id` FROM `productimages` INNER JOIN `pimagecategories` ON `productimages`.`category` = `pimagecategories`.`id` WHERE `pimagecategories`.`id` = 1;") or die(mysql_error());
 
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
Echo "<br /><br /><br /><br /><br /><br />";
//Outputs the image and other data
Echo "AKAISHI<hr><br />";
Echo "<table width='200' class='pimages' >";
 
Echo "<tr><td>".$info['thumb'] . "</b></td></tr>";
Echo "<tr><td><b> ".$info['title'] . "</b></td></tr> ";
Echo "<tr><td><a href=".$info['upjpg'] . "> HI RES JPG</a><br></td></tr>";
Echo "<tr><td><a href=".$info['uptiff'] . ">HI RES TIFF</a><br></td></tr>";
Echo '<tr><td><a href='.$info['uppng'] . '> HI RES PNG</a><br></td></tr>';
Echo "</table>";
}
?>
The code I am using was set up for

Code: Select all

$data = mysql_query("SELECT * FROM productimages") or die(mysql_error());
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Simple Query Question

Post by jackpf »

That's not what I posted 8O

Also, you're going to need to either use a wildcard, or specify those extra fields you want to display.
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Re: Simple Query Question

Post by daebat »

I think I have it figured out.
Post Reply