I am trying to do a while loop with a prepared PDO statement, but I only want it to execute if there are any rows. Currently I am using this, but it seems to be missing the first result, presumably because its moving the pointer.
What is the right way to do this?
$stmt = $pdo->prepare('SELECT * FROM products p
INNER JOIN products_to_categories c
ON p.products_id = c.products_id
WHERE c.categories_id = ?
AND products_status=?
ORDER BY p.products_sort_order,p.products_name');
$stmt->execute([$categories_id,1]);
if(($category_row = $stmt->fetch(PDO::FETCH_ASSOC)) != null) {
$no_results = count($stmt->fetch(PDO::FETCH_ASSOC));
while ($products_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// show row info
}
}