Page 1 of 1

mysql_fetch_array from several tables in query

Posted: Thu Nov 06, 2003 10:43 am
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

Posted: Thu Nov 06, 2003 10:55 am
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";

Posted: Thu Nov 06, 2003 11:03 am
by yaron
but what if the select is *.
do i have alias for each one?
then my query will be very big...

Posted: Thu Nov 06, 2003 11:11 am
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.