php 在 <path> 中调用 boolean 上的成员函数 fetch_assoc()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35669088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function fetch_assoc() on boolean in <path>
提问by dhool
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>\n";
}
?>
</tbody>
</table>
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.
当我开始实时托管它时,我才开始收到错误消息。它在我的个人计算机上运行良好,数据库连接也运行良好。
回答by trincot
The querymethod can return false
instead of a result set in case there is an error. That is why you get the error on the fetch_assocmethod call, which obviously does not exist when $resultis false
.
如果出现错误,查询方法可以返回false
而不是结果集。这就是为什么您会在fetch_assoc方法调用中出现错误的原因,当$result为时,该错误显然不存在false
。
This means you have an error in your SELECT statement. To get that error displayed, do this:
这意味着您的 SELECT 语句中有错误。要显示该错误,请执行以下操作:
$result = $conn->query($sql) or die($conn->error);
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.
很可能您对表名或列名的拼写有误。也许在移动到主机时您没有正确创建该表,并在那里拼写错误。
You should in fact see the same error when executing the same query via phpAdmin.
通过 phpAdmin 执行相同的查询时,您实际上应该看到相同的错误。
Also, replace this line:
另外,替换此行:
while(($row = $result->fetch_assoc()) !== null){
with just:
只需:
while($row = $result->fetch_assoc()) {
You could also add this for debugging:
您还可以添加此用于调试:
echo "number of rows: " . $result->num_rows;
回答by ASammour
This error happen usually when tables in the query doesn't exist. Just check the table's spelling in the query, and it will work.
当查询中的表不存在时,通常会发生此错误。只需在查询中检查表的拼写,它就会起作用。
回答by Shoaib
OK, i just fixed this error.
好的,我刚刚修复了这个错误。
This happens when there is an error in query or table doesn't exist.
当查询中有错误或表不存在时会发生这种情况。
Try debugging the query buy running it directly on phpmyadmin to confirm the validity of the mysql Query
尝试调试查询直接在phpmyadmin上运行以确认mysql查询的有效性
回答by Vivek Sharma
Please use if condition with while loop and try.
请在 while 循环中使用 if 条件并尝试。
eg.
例如。
if ($result = $conn->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
}
/* free result set */
$result->free();
}
回答by user9275836
You have to update the php.ini config file with in your host provider's server, trust me on this, more than likely there is nothing wrong with your code. It took me almost a month and a half to realize that most hosting servers are not up to date on php.ini files, eg. php 5.5 or later, I believe.
您必须在您的主机提供商的服务器中更新 php.ini 配置文件,相信我,您的代码很可能没有任何问题。我花了将近一个半月的时间才意识到大多数托管服务器的 php.ini 文件都不是最新的,例如。php 5.5 或更高版本,我相信。