I am trying to fetch the data from multiple tables "having same column names" but it doesn't give me required results in drupal. here is my code.
$result = db_query ( "select table1.symbol, table2.symbol, table1.price_sales, table2.price_sales from {table1, table2} where table1.uid = table2.uid" );
while ( $obj = $result->fetch () ) {
echo ($obj->table1.symbol); // it doesn't return the results
echo ($obj->table2.symbol);
}
fetch()
is not a method to the DatabaseStatementInterface
on Dupral 7.x
. Try fetchAllAssoc()
or fetchAllKeyed()
You should be able to change the name of any column when running the query:
$result = db_query ( "select table1.symbol as second_symbol, table2.symbol, table1.price_sales, table2.price_sales from {table1, table2} where table1.uid = table2.uid" );
while ( $obj = $result->fetch () ) {
echo ($obj->table1.second_symbol);
echo ($obj->table2.symbol);
}