MySQL left join confusion

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
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

MySQL left join confusion

Post by ben.artiss »

Hi all,

I'm having some trouble joining a value between two tables. I got my head around the LEFT JOIN x ON y=z a few weeks ago but I've come back totally confused! What I have is two tables, menu_cat and menu_items. Each row in menu_items has a cat_id which relates to the cat_id in menu_cat, where I am trying to get the title.

I swear this worked before but it's not now. What I want is to join the menu_cat.title to menu_items.cat_id, so instead of getting a numeric value I get a string. This is the query I'm using:

Code: Select all

$sql ="SELECT `menu_items`.`title`,`menu_items`.`cat_id`,`menu_items`.`description`,`menu_items`.`cost`,`menu_cat`.`title` FROM `menu_items` LEFT JOIN `menu_cat` ON `menu_items`.`cat_id`=`menu_cat`.`cat_id` WHERE `menu_cat`.`status`!=0"
But when I loop through using mysql_fetch_assoc, $row['cat_id'] is still numeric. Can anybody spot what I'm doing wrong?

Thanks, Ben
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: MySQL left join confusion

Post by requinix »

The cat_id is always going to be numeric.

JOINs will add fields to a query. Look for the title, not the cat_id.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: MySQL left join confusion

Post by ben.artiss »

Genius thanks man, but one last thing, I accessed that through $row['title'] but I also have $row['title'] in menu_items table (and am therefore losing the most important part!), is this where I would use SELECT AS or possible JOIN AS? I can feel a lot of mySQL tutorials coming...
Post Reply