CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   inner join query question (http://www.codingforums.com/showthread.php?t=280842)

shaunthomson 11-04-2012 05:18 PM

inner join query question
 
Hi

I've done inner joins before, and I figured this one would be doing the same thing as the others, but it isn't working, and I was wondering if someone could see the error?

I have 3 tables - a DJs table, a favs table, and a members table (favs table stores members favorite djs).

The favs table contains 3 columns - fav_id, member_id and the dj_id.

The DJs table contains 2 - dj_id, and name.

I'm trying to retrieve the name of each DJ that the member likes.

Code:

$sql  = "SELECT DJs.name
        FROM favs
        INNER JOIN DJs
        ON favs.dj_id = DJs.dj_id
        WHERE favs.member_id = '{$my_id}'       
        ORDER BY DJs.name";

Should that not return a result set if $my_id appears in favs, and there are matches between favs.dj_id and DJs.dj_id?

Thanks for taking a look.

Old Pedant 11-04-2012 10:01 PM

Looks right to me. So maybe the problem is in your PHP code?

Start by debugging:
Code:

$sql = "SELECT DJs.name FROM favs INNER JOIN DJs
          ON favs.dj_id = DJs.dj_id WHERE favs.member_id = '{$my_id}'
          ORDER BY DJs.name";

echo "<hr/>DEBUG SQL: " . $sql . "<hr/>";

...

Look at the debug: Does it have the right member_id value?

Oh, and if member_id is a NUMERIC field, then kill the apostrophes around $my_id.

shaunthomson 11-09-2012 01:25 AM

Wanna know what I did? I forgot to encase the query in mysql_query() !!! I've done that a few times now, and have thoroughly learned my lesson!!!


All times are GMT +1. The time now is 03:35 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.