0

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

  • You missed `` and `` inside your `` block... Try after correct your markup. – Ankit Singh Jul 25 '17 at 20:24
  • @CourtneyCunningham Try to echo something inside `if (mysqli_num_rows($result) > 0) {` outside `while` to make sure ur table names correct. – Aravindh Gopi Jul 25 '17 at 20:29
  • Are you not getting any errors? – GrumpyCrouton Jul 25 '17 at 20:41
  • @GrumpyCrouton I am not getting any kind of error. – Courtney Cunningham Jul 25 '17 at 20:44
  • Can you share your connect.php code with us, and alter it to not include your actual details? – GrumpyCrouton Jul 25 '17 at 20:46
  • @AravindhGopi I forgot to mention that but I tested it earlier in the if statement. It acts like it cannot read the table at all and is not giving any results. Every test I do it just skips down to the else and gives "0 results". I am not quite sure what to make of it. I'll upload an updated version of my code(sadly, it still gives me those same results). – Courtney Cunningham Jul 25 '17 at 20:47
  • Try to put a die() in your while loop to see if it activates at all. If it doesn't, move the die() up to the if statement to see if it activates. Neither should, as it looks like it's `if (mysqli_num_rows($result) > 0) {` that is failing – GrumpyCrouton Jul 25 '17 at 20:50
  • Yes that is the spot it is. And I just updated my code snippet. The notice was not there before you asked so I didn't notice it. – Courtney Cunningham Jul 25 '17 at 20:54
  • It doesn't even make it to the `if (mysqli_num_rows($result) > 0) {` condition. It acts like it cannot read my table. – Courtney Cunningham Jul 25 '17 at 21:01
  • @CBroe Thank you so much! That helped me pin point what needed to be fixed. Just a few dumb errors on my part lol. Thank you all so much for the help! – Courtney Cunningham Jul 25 '17 at 21:46

0 Answers0