Ekumahost Ekumahost - 1 month ago 16
PHP Question

get colums of three joined tables

I was able to join 3 tables and count the data set, bet getting the columns on specific table it only see the 3rd table column..

SELECT *
FROM `InvoiceItemTable` a
JOIN `InvoiceTable` b
ON (b.id = a.invoice)
JOIN `products` c
ON (a.product = c.id)
WHERE b.status='1' AND c.store = '2'
//$invoices = $inginger->fetch(PDO::FETCH_ASSOC))
echo $invoices['a.price'];


This price return error : Undefined index: a.price in...
there is "price" in the first table(InvoiceItemTable).
echo $invoices['invoice'];
There is invoice in first table(InvoiceItemTable) and it returns it works
I dont want to use $invoices['price'] because there is 'price' colum in the third table too and that is what it returns, I want to get the price in the first table. $invoices['price.InvoiceItemTable'] returns undefined index

Answer

php wont recognize $invoices['a.price']; you have to use $invoices['price'];

if you have same fieldname in multiple tables you have to create an alias

 SELECT a.price as a_price, b.price as b_price, c.price as c_price

and then you can use $invoices['a_price']

Comments