Skip to content

Instantly share code, notes, and snippets.

@laerciobernardo
Created February 6, 2017 18:24
Show Gist options
  • Save laerciobernardo/3b3fbd1c4357822695a510e65b72c92f to your computer and use it in GitHub Desktop.
Save laerciobernardo/3b3fbd1c4357822695a510e65b72c92f to your computer and use it in GitHub Desktop.
$stmt = $dbh->prepare('
SELECT
*
FROM
table
ORDER BY
name
LIMIT
:limit
OFFSET
:offset
');
// Bind the query params
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
// Do we have any results?
if ($stmt->rowCount() > 0) {
// Define how we want to fetch the results
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($stmt);
// Display the results
foreach ($iterator as $row) {
echo '<p>', $row['name'], '</p>';
}
} else {
echo '<p>No results could be displayed.</p>';
}
} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment