Jump to content

Mysql_fetch_assoc and joins


Zeradin

Recommended Posts

How does this work if i have

 

$leads = mysql_query('SELECT * FROM leads l JOIN accountants a ON l.to_id = a.id');
$l = mysql_fetch_assoc($leads);

and some of the columns have the same name on both tables, how do i mysql_fetch_assoc them out?

$l['a.zip']; does not work :(

Link to comment
Share on other sites

Well, you really shouldn't have fields with the same names between tables unless they are foreign keys. But, if you do, then you should only SELECT the fields that you want. If there is still a duplicate between tables and you need both values then SELECT them using a name.

 

Selecting ONLY the fields you need

SELECT l.field1, l.field2,
       a.field3, a.field4
FROM leads l
JOIN accountants a ON l.to_id = a.id

 

Selecting fields with the same name in multiple tables

SELECT l.field1 as lead_field1, l.field2 as lead_field2,
       a.field1 as acct_field1, a.field2 as acct_field2
FROM leads l
JOIN accountants a ON l.to_id = a.id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.