我在两个不同的网页上不断收到 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> - " . $idea . ' <a class="delete" href="#">[Delete]</a></span>';
}
}
错误 - 如下所示
Fatal error: Call to a member function rowCount() on a non-object in /opt/lampp/htdocs/xampp/assets/update.php on line 6
代码
$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> - " . $idea . ' <a class="delete" href="#">[Delete]</a></span>';
}
}
答案 0 :(得分:3)
将其更改为此
if(!empty($result) AND $result->rowCount() > 0)