Thursday, 22 August 2013

MySQL skipping first row in PDO query

MySQL skipping first row in PDO query

I have the following function:
function getTime ($day, $month, $year) {
try {
$data = $this->dbh->prepare("SELECT * FROM time_slots WHERE
DAY(timestamp) = :day AND MONTH(timestamp) = :month AND
YEAR(timestamp) = :year");
$data->execute(array('month' => $month,
'day' => $day,
'year' => $year));
$count = $data->fetchColumn();
if ($count) {
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
$times[] = $row;
}
foreach ($times as $time) {
echo print_r($time['timestamp']);
}
} else {
echo "There are no time slots avaliable for the date you've
chosen.";
}
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
echo "could not return data!";
}
}
It is skipping over the first row of data which should be returned by PHP.
I can't find a good answer for this, because everything I've seen on here
references the original MySQL API which is deprecated. There should be a
way around this in PDO as well though.

No comments:

Post a Comment