gpt4 book ai didi

php - fatal error : Call to a member function fetch_array() on a non-object

转载 作者:行者123 更新时间:2023-11-29 11:37:10 26 4
gpt4 key购买 nike

如何更改此代码以免收到上述错误?

这是完整的错误:

Fatal error: Call to a member function fetch_array() on a non-object in /home/bestc165/public_html/opinuo.com/stream.php on line 10

<?php 
$link = mysqli_connect('localhost', 'bestc165_opinuo', 'opinuo');

$sql_string = 'SELECT * FROM debates LIMIT 2 order by ts ';

$result = mysqli_query($link,$sql_string );

while($row = $result->fetch_array())
{
$rows[] = $row;
}

foreach($rows as $row)
{
$name = $row['name'];
$text = $row['text'];
?>

<h2><?php echo $name;?></h2>
<p><?php echo $text;?></p>

<?php

}

?>

最佳答案

LIMIT 位于 ORDER BY

之后

将您的查询更改为

$sql_string = 'SELECT * FROM debates ORDER BY ts LIMIT 2';

mysqli_query 在失败时返回 FALSE,因此您的 $result 不是一个对象,而是一个 boolean 因此您的错误.

更新

如评论中所述,请确保在连接中传递数据库名称

$link = mysqli_connect('host', 'username', 'password', 'databaseName');

或者使用mysqli_select_db

mysqli_select_db($link, "databaseName");

关于php - fatal error : Call to a member function fetch_array() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36385536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com