← CodePrimer
PHP Intermediate Database

PDO fetch all rows

How do I list records from MySQL?

prepare + fetchAll.

Sample
<?php
$stmt = $pdo->query('SELECT id, name, email FROM students ORDER BY id DESC');
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
  echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8');
}

Notes

Escape output when printing into HTML.

Keywords: select · fetchAll · list · pdo

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share