Reputation:
So I've seen this done before, but here's my issue. I'm trying to see if the user posted a post, and if not I'd display a message. The issue is I never seem to get the message. Here's How I'm querying
$posts = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$posts[] = array(
'post_content' => $row['post_content'],
'likes' => $row['post_likes'],
'time' => $row['post_date'],
'id' => $row['id']
);
}
And how I'm trying to count
<? if(count($posts) < 1): ?>
<h1>No posts</h1>
<? endif; ?>
No errors, and I have no records in the DB either.
Upvotes: 0
Views: 32
Reputation:
This line:
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
will assign false
to $row
after the last row has been read. Notwithstanding that this is not an array, the next few lines will assign something to the next element of $posts
Thus you'll always get at least one element in $posts
and you won't see the message. See this codepad
You should test the fetch
for false and only process it if it's not.
Change your fetch line to this:
while(($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false){
Upvotes: 1