I am trying to connect to the database and print the table on a webpage. My database connection is fine but it won't connect to the table. I cannot figure out why there is no connection to the table. It keeps returning 'no result' or '0 results' and my table is not empty.
Here is the code I am using:
<!doctype html>
<html>
<head>
<title>Inventory Table</title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db";
// Create connection
$connect = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}
//execute the SQL query and return records
$sql = "SELECT * FROM Inventory";
$result = $connect->query ( $sql );
if ($result->num_rows > 0) {
echo "<table border='2'>
<tr>
<th>Lot_Num</th>
<th>Beer_Name</th>
<th>Inventory_Date</th>
<th>Num_Cases16</th>
<th>Num_Cases12</th>
<th>Num_Sixtel</th>
<th>Num_HalfBBL</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc () ) {
echo "<tr>";
echo "<td>" . $row["Lot_Num"] . $row["Beer_Name"] . $row["Inventory_Date"] . $row["Num_Cases_16oz"] . $row["Num_Cases_12"] . $row["Num_Sixtel_Kegs"] . $row["Num_HalfBBL_Kegs"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($connect);
echo "</table>";
?>
</body>
</html>
This is the new error I am getting:
Notice: Trying to get property of non-object in /var/www/html/inventory_table.php on line 28
0 results
Line 28 is the beginning of the if else statement containing the while loop.
Any help is appreciated, thank you!!