1//Instantiate the PDO object and connect to MySQL.
2$pdo = new PDO(
3 'mysql:host=127.0.0.1;dbname=my_database',
4 'username',
5 'password'
6);
7
8//The COUNT SQL statement that we will use.
9$sql = "SELECT COUNT(*) AS num FROM users";
10
11//Prepare the COUNT SQL statement.
12$stmt = $pdo->prepare($sql);
13
14//Execute the COUNT statement.
15$stmt->execute();
16
17//Fetch the row that MySQL returned.
18$row = $stmt->fetch(PDO::FETCH_ASSOC);
19
20//The $row array will contain "num". Print it out.
21echo $row['num'] . ' users exist.';
22