What is the difference between using fetch and fetchAll in PDO for database queries in PHP?

When using PDO for database queries in PHP, the main difference between fetch and fetchAll is in how they retrieve results from a query. fetch is used to retrieve a single row from the result set, while fetchAll retrieves all rows. If you only need to retrieve one row at a time, you would use fetch. If you need to retrieve multiple rows, you would use fetchAll.

// Using fetch to retrieve a single row
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute(['id' => 1]);
$row = $stmt->fetch();

// Using fetchAll to retrieve multiple rows
$stmt = $pdo->prepare("SELECT * FROM users");
$stmt->execute();
$rows = $stmt->fetchAll();