get row count of statement mysql php $stmt-> code example
Example: number of rows in a mysql table pdo
//Instantiate the PDO object and connect to MySQL.
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=my_database',
'username',
'password'
);
//The COUNT SQL statement that we will use.
$sql = "SELECT COUNT(*) AS num FROM users";
//Prepare the COUNT SQL statement.
$stmt = $pdo->prepare($sql);
//Execute the COUNT statement.
$stmt->execute();
//Fetch the row that MySQL returned.
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//The $row array will contain "num". Print it out.
echo $row['num'] . ' users exist.';