How should I solve this MySql problem (PHP) ? (Beginner)
Posted
by Camran
on Stack Overflow
See other posts from Stack Overflow
or by Camran
Published on 2010-04-20T18:04:17Z
Indexed on
2010/04/20
18:13 UTC
Read the original article
Hit count: 140
I have several tables in a MySql database.
I have a classifieds website, and at the bottom I display the users last visited classifieds. I do this by storing the ID:s of the ads to an array in the cookie.
Now, my db is made up like this kindof:
Main Table: // Stores global information, ie these fields have to be filled out in every record, never be blank
ID
Price
category
Seller
Item Table: // Stores descriptive info about whats for sale
ID
AD_ID (FK) //This is the same as ID in the MAIN TABLE
Color
Size
Mileage etc
My problem is that I need to know what category the ad is in, in order to query mysql for the right information I think.
So I need two variables, but the cookie only has one (ID) stored.
Offcourse I could make two queries, first one just matching the ID to the main_table and fetch the category from the Main_table. Then make the second query and fetch all other info from the right table.
Here is an example if the category was Vehicles:
SELECT * FROM main_table, vehicles_table, WHERE main_table.id=$id_from_cookie AND main_table.ad_id=vehicles_table.ad_id
As you can see above, I need the category to write in what table to check, right?
But I think there must be a smarter way, like fetching them in one single query using only one variable (id from cookie)? How should I do this?
Understand?
Let me know if you need more input...
Thanks
© Stack Overflow or respective owner