Page 1 of 1

mysql_assoc_array( question.(probably very simple)

Posted: Thu Aug 11, 2005 8:35 am
by HenryS
ok, am building a little php app for uni (this probably seems like a very noobish question ,but I only started php about a month ago )

Problem is, I am selecting data from a mysql database, and the query left-joins two tables. now, I have the same column-name in each table (u_id). When i try to spit out the associated value of u_id after putting the query result into an associated array, it gives me the value of the second u_id and not the first, which is not what I want.

How do I tell the array to select the first u_id column and not the second?

thanks,
Henry

Posted: Thu Aug 11, 2005 8:47 am
by neophyte
Query aliases?

Code: Select all

$sql = "SELECT a.u_id, a.field2, b.field2  
            FROM users a 
            LEFT JOIN some_table b 
             ON a.field2 = b.field2";
If your ON statement is on the id. I don't have any idea. You may need to rethink you database/query.

Posted: Thu Aug 11, 2005 9:11 am
by feyd
you can either get specific about what you select from the tables (sort of), or not use the associative fetch. A numeric fetch will have all fields in order of selection. You can retrieve the names using other database function calls.

The first can also be done using a RIGHT JOIN, where you place the original first table on the right, instead of the left.

Posted: Thu Aug 11, 2005 6:15 pm
by HenryS
Thanks so much feyd, that works!

Had to change my query around a bit but using a numeric array gives me the right one. :)

Thanks again for your great assistance!