gpt4 book ai didi

php - 非对象错误上的函数 rowCount()

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:13 26 4
gpt4 key购买 nike

我在两个不同的页面上不断收到 function rowCount() on a non-object 错误。知道为什么会发生这种情况以及我如何更改代码以防止它发生吗?

错误-如下

Fatal error: Call to a member function rowCount() on a non-object in /opt/lampp/htdocs/xampp/index.php on line 35

代码:

$sql = "SELECT * from idea ORDER BY datetime DESC LIMIT 50;";
$result = $pdo->query($sql);

if($result->rowCount() > 0 && !empty($result)) // line 35
{
foreach ($result as $row)
{
$id = $row['id'];
$title = $row['title'];
$idea = $row['idea'];

echo '<span class="idea" id="'.$id.'"><strong style="color: #0081C5">' . $title . "</strong>&nbsp;-&nbsp;" . $idea . '&nbsp;<a class="delete" href="#">[Delete]</a></span>';
}
}

错误 - 如下所示

fatal error :在第 6 行的/opt/lampp/htdocs/xampp/assets/update.php 中的非对象上调用成员函数 rowCount()

代码

 $sql = "SELECT * from idea ORDER BY datetime DESC LIMIT 50;";
$result = $pdo->query($sql);

if($result->rowCount() > 0 && !empty($result)) // line 6
{
foreach ($result as $row)
{
$id = $row['id'];
$title = $row['title'];
$idea = $row['idea'];

echo '<span class="idea" id="'.$id.'"><strong style="color: #0081C5">' . $title . "</strong>&nbsp;-&nbsp;" . $idea . '&nbsp;<a class="delete" href="#">[Delete]</a></span>';
}
}

最佳答案

改成这样

if(!empty($result) AND $result->rowCount() > 0)

关于php - 非对象错误上的函数 rowCount(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18005397/

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