問題描述
我在運(yùn)行以下代碼以顯示從數(shù)據(jù)庫(kù)進(jìn)行的預(yù)訂時(shí)遇到上述錯(cuò)誤.
I'm getting the above error when running the below code to display bookings made from a database.
<?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>
";
}
?>
</tbody>
</table>
當(dāng)我開始實(shí)時(shí)托管它時(shí),我才開始收到錯(cuò)誤消息.它在我的個(gè)人電腦上運(yùn)行良好,數(shù)據(jù)庫(kù)連接也運(yùn)行良好.
I only started to receive the error when i started hosting it live. It works fine on my personal computer, the databse connection works fine also.
推薦答案
query 方法可以返回 false
而不是結(jié)果集,以防出現(xiàn)錯(cuò)誤.這就是為什么您會(huì)在 fetch_assoc 方法調(diào)用中出現(xiàn)錯(cuò)誤的原因,當(dāng) $result 為 false
時(shí),這顯然不存在.
The query method can return false
instead of a result set in case there is an error. That is why you get the error on the fetch_assoc method call, which obviously does not exist when $result is false
.
這意味著您的 SELECT 語(yǔ)句有錯(cuò)誤.要顯示該錯(cuò)誤,請(qǐng)執(zhí)行以下操作:
This means you have an error in your SELECT statement. To get that error displayed, do this:
$result = $conn->query($sql) or die($conn->error);
很可能您對(duì)表名或列名的拼寫有誤.可能在移動(dòng)到主機(jī)時(shí)您沒有正確創(chuàng)建該表,并在那里拼寫錯(cuò)誤.
Most probably you have a wrong spelling for the table name or a column name. Maybe when moving to the host you did not create that table correctly, and made a spelling mistake there.
實(shí)際上,當(dāng)您通過 phpAdmin 執(zhí)行相同的查詢時(shí),您應(yīng)該會(huì)看到相同的錯(cuò)誤.
You should in fact see the same error when executing the same query via phpAdmin.
另外,替換這一行:
while(($row = $result->fetch_assoc()) !== null){
只要:
while($row = $result->fetch_assoc()) {
你也可以添加這個(gè)用于調(diào)試:
You could also add this for debugging:
echo "number of rows: " . $result->num_rows;
這篇關(guān)于在 <path> 中的 boolean 上調(diào)用成員函數(shù) fetch_assoc()的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!