BuildMyWeb Posted February 14, 2013 Share Posted February 14, 2013 the text in red is where im confused. how do i assign values for $orders_id and $shipping_id from their respective tables in one query? i imagine it can be done. i dont have to do 2 separate queries, do i? $query_order = "SELECT orders.id, shipping.id FROM orders, shipping WHERE orders.order_num = shipping.order_num AND orders.order_num = \"$order_req\""; $result_order = $db->query( $query_order ); $num_rows_order = $result_order->num_rows; for( $i=0; $i < $num_rows_order; $i++ ) { $row_order = $result_order->fetch_assoc(); // put field values for the row in an array $orders_id = $row_order['id']; $shipping_id = $row_order['id']; } Link to comment https://forums.phpfreaks.com/topic/274500-selecting-identically-named-rows-in-two-tables/ Share on other sites More sharing options...
Barand Posted February 15, 2013 Share Posted February 15, 2013 select column aliases Select orders.Id as oid... Link to comment https://forums.phpfreaks.com/topic/274500-selecting-identically-named-rows-in-two-tables/#findComment-1412528 Share on other sites More sharing options...
BuildMyWeb Posted February 15, 2013 Author Share Posted February 15, 2013 thanks Barand. so like this? $query_order = "SELECT orders.id AS oid, shipping.id AS sid FROM orders, shipping WHERE orders.order_num = shipping.order_num AND orders.order_num = \"$order_req\""; $result_order = $db->query( $query_order ); $num_rows_order = $result_order->num_rows; for( $i=0; $i < $num_rows_order; $i++ ) { $row_order = $result_order->fetch_assoc(); // put field values for the row in an array $orders_id = $row_order['oid']; $shipping_id = $row_order['sid']; } Link to comment https://forums.phpfreaks.com/topic/274500-selecting-identically-named-rows-in-two-tables/#findComment-1412532 Share on other sites More sharing options...
Barand Posted February 15, 2013 Share Posted February 15, 2013 Yes, and no Aliases are right. If you are only expecting a single row then you don't want a for loop. If you expect several rows the ids will be overwritten in each loop and you will only get the last ones Link to comment https://forums.phpfreaks.com/topic/274500-selecting-identically-named-rows-in-two-tables/#findComment-1412547 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.