$stmt = $pdo->prepare('SELECT name, email FROM users WHERE id = ?'); $stmt->execute([$id]); $row = $stmt->fetch(); $name = $row['name']; $email = $row['email'];
$stmt = $pdo->prepare('SELECT COUNT(*) as count FROM users'); $stmt->execute(); $row = $stmt->fetch(); $count = $row['count'];This example demonstrates how fetch() can be used to retrieve a single value, in this case the count of all rows in the users table. The SELECT statement uses the COUNT function to retrieve a single value as count, which we then retrieve and assign to the $count variable using fetch(). Package library: PDO (PHP Data Objects) is a built-in PHP extension that provides an interface to access databases in PHP.