Forum Moderators: coopster

Message Too Old, No Replies

specifying fields in different tables

         

dave1236

8:09 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



All:

I am having an issue in trying to properly display the results of a query where i query two tables...

I think the issue is that both tables have a field named 'id'...

My query seems to return the proper results, and is as follows:

$query = "SELECT * FROM NewLinks2,Newstores WHERE NewLinks2.store = Newstores.advertiser AND DATE_SUB(CURDATE(),INTERVAL 5 DAY) <= entry AND entry <= '$mydate' AND expire > '$today' ORDER BY NewLinks2.id";

However, when I display:

<a href='dealview2.php?id=$id'>$linktext</a>

the value of id is all screwed up?

How do I specify which table the value for id should show up?

thanks!

David

whoisgregg

8:39 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For this reason, I've gotten in the habit of prepending all column names for a particular table with a unique string. So, in this case, my id fields would be called "nl2_id" and "ns_id" (or something similar).

However, you can also do this in the query:

$query = "SELECT `NewLinks2`.`id` AS nl2_id,`Newstores `.`id` AS ns_id,* FROM NewLinks2,Newstores WHERE NewLinks2.store = Newstores.advertiser AND DATE_SUB(CURDATE(),INTERVAL 5 DAY) <= entry AND entry <= '$mydate' AND expire > '$today' ORDER BY NewLinks2.id";

Incidentally, make sure you are escaping any user input (like $mydate) before sending it to the database. :)