mysql_fetch_array from several tables in query

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
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

mysql_fetch_array from several tables in query

Post by yaron »

Hello all,
I have a query which involves several tables that have the same fields.
How can i differ the field name in the mysql_fetch_array function?

suppose i have a date field in table1 and table2
when i write

Code: Select all

$result=mysql_query($sql);
$resultAraay=mysql_fetch_array($result)
echo $resultAraay['date'];
?>
which one will it give table1 date or table2 date? how can access them both?

Thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you can use the integer index: $resultArray[<some_integer>] or alias the field in sql query:

Code: Select all

select something, table1.field as t1field, table2.field as t2field from table1,table2 where .... ;
then you can access it as usually:

Code: Select all

echo $resultArray["t1field"]."\n";
   echo $resultArray["t2field"]."\n";
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

but what if the select is *.
do i have alias for each one?
then my query will be very big...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Use integer index. Usually it's bad idea to select *, as it generates extra load on mysql.

you can also do something like this:
select *, table1.field as t1field, table2.field as t2field......

I.e. select all and alias only fields with duplicate names... this will generate even more load but will shorten your query.
Post Reply