Click to See Complete Forum and Search --> : query from 3 tables


rbailer
08-31-2006, 09:57 AM
This query works when i use two of the tables, but when it try to go to all 3 it doesnt work...

$sresult = mysql_query("SELECT * from docutech, tblorder, tblmainorder where docutech.orderid = tblorder.autoid and tblmainorder.orderid = tblorder.id");

This looks good to be, so maybe it is the way I am printing the data...

while($elist=mysql_fetch_object($sresult))
{
....code.... $elist->autoid ....code....
}

NogDog
08-31-2006, 10:26 AM
You might want to debug with a print_r($elist) within the while loop to see what array indexes are actually being assigned to the selected columns.

Generally with mutli-table queries, I find it better to not use "*", but instead explicitly list the columns I want with the table prefix on each, and use "AS" to give an alias, which I then know will be the value used for the array indexes:

SELECT doc.orderid AS doid, to.autoid AS toaid, tm.orderid AS tmoid, <etc....>
FROM docutech doc, tblorder to, tblmainorder tm WHERE <etc....>

In this case, I'll now know that autoid is $elist->toaid, for example.