Is it good way for create array from invoice data and invoice items with php and mysql for Angularjs or i can mix two query inside one query and make this array variable ?
I want have array with this elemets id
, payPrice
, payStatus
, clientAddress
and nested array with items
that contant invoice intems data.
$result = $db->query("select `id`,`payPrice`,`payStatus`,`clientAddress`
from `invoice` where `clientId`=22");
if($result && $result->num_rows>0){
if($row=$result->fetch_assoc()){
$result1 = $db->query("select * from `invoice_items` where `invoiceId`=". $row['id']);
if($result1 && $result1->num_rows>0){
$row["items"]=$result1;
return $row;
}else{return false;}
}else{return false;}
}else{return false;}
You cannot create an array datatype in MySQL, a join will get all of the data, but it will be all of the items with the invoice attached to each record.
For example if you have the following tables and data
INVOICE TABLE
[ id | name | date ]
[ 1 | Stephen | 30th November 2016 ]
ITEMS TABLE
[ id | invoice_id | name | price ]
[ 1 | 1 | Wine | 12.99 ]
[ 2 | 1 | Cheese | 4.99 ]
Then a join query like this
SELECT
`invoice`.`*`,
`item`.`*`
FROM
`invoices` AS `invoice`
INNER JOIN
`items` AS `item` ON (`invoice`.`id` = `item`.`invoice_id`);
Will give you the follow rows
id | name | date | id | invoice_id | name | price
1 | Stephen | 30th November 2016 | 1 | 1 | Wine | 12.99
1 | Stephen | 30th November 2016 | 2 | 1 | Cheese | 4.99
I don't think this is what you are looking for, but you could use it either way. Anyhow, here is a snippet of your code to get the array with a bit more fail first (not as many indentations)
$query = $db->query("SELECT `id`, `payPrice`, `payStatus`, `clientAddress` FROM `invoice` WHERE `clientId` = 22");
if(! $query || $query->num_rows != 1) {
return false;
}
$invoice = $query->fetch_assoc();
$items = $db->query("SELECT * FROM `invoice_items` WHERE `invoiceId` = {$invoice['id']}");
if(! $items || $items->num_rows < 1) {
return false;
}
$invoice['items'] = $items->fetch_assoc();
return $invoice;
Yes, i think you selected good techniqe for make array from tables . Because if you join two table together each row contain two table columns and you can't nested.
You are reusing the $result
variable in the second query and not actually fetching the data from the second query.
function xxx()
{
$inv = $db->query("select `id`,`payPrice`,`payStatus`,`clientAddress`
from `invoice` where `clientId`=22");
if($inv && $inv->num_rows > 0){
// should only ever be one of these
if($invoice = $inv->fetch_assoc()){
$items = $db->query("select *
from `invoice_items`
where `invoiceId` = {$invoice['id']}");
if($items && $items->num_rows > 0){
// loop round possibly many items rows
while ($item = $items->fetch_assoc()) {
$invoice["items"][] = $item;
}
return $invoice;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.