我在代码下面运行hte时显示上述错误,以显示从数据库进行的预订。
<?php
$servername = "localhost";
$username = "*********";
$password = "********";
$dbname = "thelibr1_fyp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, tablename, numseats, person FROM confirms";
$result = $conn->query($sql);
?>
<table id="Confirms" border ="2" style="length:900px;width:350px;">
<thead>
<tr style= "background-color: #A4A4A4;">
<td>Booking ID:</td>
<td>Table No.:</td>
<td>No. of Seats:</td>
<td>Person:</td>
</tr>
</thead>
<tbody>
<?php
while(($row = $result->fetch_assoc()) !== null){
echo
"<tr>
<td>{$row['id']}</td>
<td>{$row['tablename']}</td>
<td>{$row['numseats']}</td>
<td>{$row['person']}</td>
</tr>\n";
}
?>
</tbody>
</table>
当我开始实时托管它时,我才开始收到错误,在我的个人电脑上它工作正常。数据库连接也可以正常工作。
答案 0 :(得分:16)
查询方法可以返回false
而不是结果集,以防出现错误。这就是为什么你在 fetch_assoc 方法调用上得到错误的原因,当 $ result 是false
时,这显然不存在。
这意味着您的SELECT语句中有错误。要显示该错误,请执行以下操作:
$result = $conn->query($sql) or die($conn->error);
很可能您的表名或列名拼写错误。也许当你搬到主机时你没有正确地创建那个表,并在那里拼错了。
通过phpAdmin执行相同的查询时,您实际上应该看到相同的错误。
另外,请替换此行:
while(($row = $result->fetch_assoc()) !== null){
只是:
while($row = $result->fetch_assoc()) {
您也可以将其添加到调试中:
echo "number of rows: " . $result->num_rows;
答案 1 :(得分:4)
当查询中的表不存在时,通常会发生此错误。只需在查询中检查表的拼写,即可使用。
答案 2 :(得分:-1)
请使用while循环的条件并尝试。
例如
if ($result = $conn->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
}
/* free result set */
$result->free();
}
答案 3 :(得分:-1)
好的,我只是解决了这个错误。
当查询中存在错误或表不存在时会发生这种情况。
尝试调试查询购买直接在phpmyadmin上运行以确认mysql查询的有效性
答案 4 :(得分:-2)
你必须在你的主机提供商的服务器上更新php.ini配置文件,相信我,你的代码可能没有任何问题。我花了将近一个半月的时间才意识到大多数托管服务器都不是最新的php.ini文件,例如。我相信php 5.5或更高版本。