php pdo select count example
Example 1: sql row count php pdo
<?php
$del = $dbh->prepare('DELETE FROM fruit');
$del->execute();
print("Return number of rows that were deleted:\n");
$count = $del->rowCount();
print("Deleted $count rows.\n");
?>
Example 2: number of rows in a mysql table pdo
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=my_database',
'username',
'password'
);
$sql = "SELECT COUNT(*) AS num FROM users";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['num'] . ' users exist.';