← CodePrimer
PHP
Intermediate
Database
PDO fetch all rows
How do I list records from MySQL?
prepare + fetchAll.
<?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
PHP
Embed PHP inside HTML
How do I mix PHP and HTML?
SQL
CREATE DATABASE
How do I create a MySQL database?
PHP
PHP comments
How do I write comments in PHP?
SQL
ALTER TABLE add column
How do I add a column to an existing table?
PHP
PHP variable rules
What are the rules for PHP variables?
SQL
Foreign key relationship
How do I relate posts to users?