Tuesday 17 December 2019

php - Connection to the database works but won't query

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:




   


Inventory Table



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 "









";
// output data of each row
while($row = $result->fetch_assoc () ) {

echo "";
echo "
Lot_Num Beer_Name Inventory_Date Num_Cases16 Num_Cases12 Num_Sixtel Num_HalfBBL
" . $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"] . "
";
}
} else {
echo "0 results";
}

mysqli_close($connect);

echo "
";

?>





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!!

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...