Skip to content

Instantly share code, notes, and snippets.

@terrymun
Last active August 29, 2015 14:27
Show Gist options
  • Save terrymun/9c6dece0271e072e561b to your computer and use it in GitHub Desktop.
Save terrymun/9c6dece0271e072e561b to your computer and use it in GitHub Desktop.
Count the number of rows returned
<?php
// Assuming that database connection is already open
// Prepare statement
$stmt = $db->prepare("SELECT user, email, country FROM users WHERE id > 1000");
$stmt->execute();
// Get results
if($stmt->rowCount() > 0) {
// One or more rows returned, start iteration through set
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// Do something for each row
}
} else {
// No rows returned
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment