Complex SQL query... names of returned variables

Posted by Andrew P. on Stack Overflow See other posts from Stack Overflow or by Andrew P.
Published on 2010-03-25T01:19:52Z Indexed on 2010/03/25 1:23 UTC
Read the original article Hit count: 474

Filed under:
|
|

Excuse me for what I'm sure is an elementary question for most of you, but I have an issue with table columns from separate tables having the same name as one another, and trying to select from both tables in the same query.

Okay, so this is my code:

$q_value = $mdb2->quote($_POST['query']);
$field = $_POST['field'];
$sql = "SELECT m.*, l.name FROM memberlist m, mail_lists l
        WHERE m.$field=$q_value
        AND l.id = m.list
        ORDER BY m.id";
$l_list = $mdb2->queryAll($sql, '', 'MDB2_FETCHMODE_ASSOC');

The table memberlist has the following columns: id, email, list, sex, name

and the table mail_lists has the following columns: id, name

After running the query, I later loop through the results with a foreach like so:

foreach ($l_list as $l){ //blahblah }

The problem is that the column 'name' in mail_lists refers to the names of the list, while the column 'name' in memberlist refers to the name of the member.

When I later access $l->name (within the foreach), will I get m.name, or l.name? Furthermore, how do I get access to the other?

Or will I just have to do two separate queries?

© Stack Overflow or respective owner

Related posts about php

Related posts about sql