Page 1 of 1

fetching from join

Posted: Mon Jan 08, 2007 2:59 am
by kpraman
Hello,

I am trying to fetch values from two tables with the help of join statements. if the two tables are having same field name(which i am not going to use in joining). how to differeciate them and get their values.

I am getting the wrong values.


Code: Select all

$query=mysql_query("SELECT * FROM tbl1, tbl2 WHERE tbl1.id=tbl2.id");
   while($r=mysql_fetch_array($query))
     {
          $fetch[]=$r;
     }
tbl1 and tbl2 contains field name enableFlag.

i want to get tbl1 enableFlag value and also tbl2 enableFlag. How to do this?

Thanx

Posted: Mon Jan 08, 2007 3:35 am
by onion2k
The best way (in my opinion) is to alias the column names.

Code: Select all

$query=mysql_query("SELECT *, tbl1.enableFlag as tbl1_enableFlag, tbl2.enableFlag as tbl2_enableFlag FROM tbl1, tbl2 WHERE tbl1.id=tbl2.id");
   while($r=mysql_fetch_array($query))
     {
          $fetch[]=$r;
     }
You'll then have a couple of fields called tbl1_enableFlag and tbl2_enableFlag respectively that contain the data from each table.

Posted: Mon Jan 08, 2007 5:47 am
by kpraman
Thanx.