Reputation: 136
I have a while loop after a PDO statement. My code is below.
$query = "SELECT * FROM private_messages WHERE to_id = :my_id AND to_email = :email AND recipient_delete='0' ORDER BY time_sent DESC";
$stmt = $dbo->prepare($query);
$stmt->execute(array(":my_id" => $my_id, ":email" => $email));
$row = $stmt->fetch();
while($row = $stmt->fetch()){ ...
The outputted results are always missing one of the rows, and so the most recent private message received by the user never shows. I have a notifier which tells the user how many new messages they have, which is counting correctly (it shows that they have one more message than the actual message list outputs). I cannot find the answer using Google and it is driving me crazy. This is my first ever try at programming in any kind of language since the early days of BASIC in the 80s so I am spending all of my time reading up about things, but cannot find anything to help me with this issue. Can anyone tell me where I am going wrong please? Thanks!
Upvotes: 4
Views: 4457
Reputation: 12836
Remove the line:
$row = $stmt->fetch();
It puts the first line inside $row
Then when you do:
while($row = $stmt->fetch()){ ...
Its starts fetching from the second line onwards, and the first line you fetched gets lost which is why you are always missing only one line.
Upvotes: 6