I have the following function which pulls in invoice data from tables invoices, however how can I get data from the customers table matching up invoice number:
So I guess need to get * from customers tables where invoice = invoice
?
PHP
// the query
$query = "SELECT * FROM invoices ORDER BY invoice ASC";
// mysqli select query
$results = $mysqli->query($query);
// mysqli select query
if($results) {
print '<table class="table table-striped table-bordered" id="data-table" cellspacing="0"><thead><tr>
<th><h4>Invoice</h4></th>
<th><h4>Customer</h4></th>
<th><h4>Issue Date</h4></th>
<th><h4>Due Date</h4></th>
<th><h4>Status</h4></th>
<th><h4>Action</h4></th>
</tr></thead><tbody>';
while($row = $results->fetch_assoc()) {
print '
<tr>
<td>'.$row["invoice"].'</td>
<td>'.$row["customer_name"].'</td>
<td>'.$row["invoice_date"].'</td>
<td>'.$row["invoice_due_date"].'</td>
<td>test</td>
<td><a href="invoice-edit.php?id='.$row["invoice"].'" class="btn btn-primary">Edit</a> <a data-invoice-id="'.$row['invoice_id'].'" class="btn btn-danger delete-product">Delete</a></td>
</tr>
';
}
print '</tr></tbody></table>';
} else {
echo "<p>There are no invoices to display.</p>";
}
try this..
$query = "SELECT c.customer_name, i.* FROM invoices as i JOIN customers as c ON i.customer_id = c.id ORDER BY i.invoice ASC";
this will give you customer name as you require.
$query = "SELECT *
FROM Invoices I, Customers C
WHERE I.invoice = C.invoice
ORDER BY C.invoice ASC";
If you would prefer a statement without joins..
First of all, it's generally a better idea to include your keys in your select
(and it's also easier to troubleshoot) instead of *
. However, assuming that these are your fields, you can connect these using a JOIN
like so:
SELECT *
FROM invoices i
JOIN customer c
ON c.invoice = i.invoice
ORDER BY i.invoice
Assuming you have a common key on each table, which as you suggest, is invoice (this is the huge advantage of using a database as opposed to a flat file), you can just join them together (the i
and c
are just shortcuts which make it easier to read the query).
You can also modify this with a WHERE
after the ON
row to narrow down your results, such as something like this (assuming you are looking for a specific invoice numbered 12345
SELECT *
FROM invoices i
JOIN customer c
ON c.invoice = i.invoice
WHERE i.invoice = 12345
ORDER BY i.invoice
SQL is pretty powerful. If you want to read up more, there are some great tutorials here on this site: http://www.w3schools.com/sql/
If you want to call this in your query (using the first example) you can just do it the same way you were, like so:
while($row = $results->fetch_assoc()) {
echo $row["invoice"];
}
Edit: In the future it is better to declare your keys in the select. Assuming that customer_name
is in the customer table, you would call it as c.customer_name
in the select. However, here's how it would work with the wildcard to get everything from both tables. Change your query to this:
SELECT i.*, c.*
FROM invoices i
JOIN customer c
ON c.invoice = i.invoice
ORDER BY i.invoice
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.