- Forums
- PHP
- How To Query Two Tables In One Mysql Database Query
This Page Contains information about How To Query Two Tables In One Mysql Database Query By wallpaperama in category PHP with 1 Replies. [1211], Last Updated: Sat May 18, 2024
wallpaperama
Wed Nov 26, 2008
1 Comments
5138 Visits
ok, if you are like me, i used to send two queries when i need to find out an associated field with another one. for example, lets say i have a wallpaper site and i have a category and the wallpaper. now each wallpaper will have to be organized into each parent category. like this for example:
Categories:
Animals (CatId=1)
- Lions
- Bears
- Dogs
Cars (CatId=2)
- Ferrari
- Ford
- Nissan
- Chevy
- Toyota
as you can see from my example above, i have two categories, one is Animals, which the CatId=1 and the other one is Cars which has a CatId=2 . and each categories has its wallpapers.
now lets say the user is in the Lions page, but i want to also get the Lions Category in this case, Animals. that way i can say
This Lion Wallpaper is Found in the Animals Category
ok, so you will understan what im talking about here lets say the url page i am on is:
http://www.wallpaperama.com/page.php?CatId=2
this is how my sql query will look like:
$sql ="SELECT c.CatName, w.WallpaperName FROM CategoryTable c, WallpaperTable w WHERE CatId = $_GET[CatId]";
now with this, i can get the values from two different tables in one shot using this php mysql code.
i hope with this script you can learn something new, this way you can avoid sending two queries, because the less queries you do, the better