与数据库的连接有效,但不会查询

时间:2017-07-25 20:14:21

标签: php database mysqli

我正在尝试连接数据库并在网页上打印表格。我的数据库连接很好,但它不会连接到表。我无法弄清楚为什么没有连接到表。它不断返回“无结果”或“0结果”,我的表格不为空。

以下是我正在使用的代码:

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

这是我得到的新错误:
注意:尝试在第28行的/var/www/html/inventory_table.php中获取非对象的属性 0结果
第28行是包含while循环的if else语句的开头。

感谢任何帮助,谢谢!!

0 个答案:

没有答案
相关问题