I'm trying to create an HTML table using PHP and MySQL. I have 2 MySQL tables, named coffees
, and suppliers
, seen here:
Coffees:
In my HTML table, I wish to display the coffee name, supplier name, and total.
My problem however, is that in order to display the supplier name, I must use the SUP_ID
field from the coffees
table to reference SUP_NAME
from the suppliers
table.
My final output aims to be something along the lines of this:
My code is the following:
<?php
$connection_var = new mysqli("server", "user", "pw", "db");
if (mysqli_connect_errno()) {
printf("Connection failed: s\n", mysqli_connect_errno());
exit();
}
$coffees = "SELECT * from coffees";
$suppliers = "SELECT * from suppliers";
$coffeesArr = mysqli_fetch_array($coffees);
$supplierArr = mysqli_fetch_array($suppliers)
echo("<br>");
echo("<table border='1'>");
echo("<tr><td>COF_NAME</td><td>SUPPLIER NAME</td><td>TOTAL</td></tr>");
foreach($resultArr as $row) {
echo("<tr>");
echo("<td>" . $row['COF_NAME'] . "</td><td>" . /* Supplier name (from 2nd table) */ . "</td><td>" . $row['TOTAL'] . "</td><td>");
echo"</tr>");
}
mysqli_close($connection_var);
?>
As seen above, I can easily reference the COF_NAME
and TOTAL
fields from the first table, however I am at a loss at how to reference the SUP_NAME
field from my 2nd MySQL table, as it would require me to use the SUP_ID
field from the first table. I would greatly appreciate if someone could guide me on how to achieve this.