Another point to keep in mind is that fetch_assoc() may not return every column that you expect. If 2 output columns have the same name, only the last instance of this column will be obtained using fetch_assoc() . For example, the following query:
SELECT * FROM orders LEFT JOIN line_items ON orders.id = line_items.order_id
Assuming both tables have a column named id , the resulting associative array will have a key named id whose value is line_items.id , and you cannot extract orders.id from the result!
You can work around this problem with duplicate column names and continue to use fetch_assoc() by manually aligning SELECT columns, giving each column a unique alias:
SELECT o.id AS order_id, #unique alias i.id AS line_item_id, #unique alias o.date, i.qty, i.price FROM orders o LEFT JOIN line_items i ON i.order_id = o.id
Or you can switch to using fetch_array() , which will allow you to access id by index rather than by key
rmirabelle
source share