function rowCount() on a non-object
2 つの異なるページでエラーが発生し続けます。なぜこれが起こっているのか、コードを変更してそれを防ぐ方法はありますか?
エラー - 以下のように
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>';
}
}