Skip to content

Instantly share code, notes, and snippets.

@muathendirangu
Created March 3, 2024 19:41
Show Gist options
  • Save muathendirangu/d57ef7b086af1747e115eea64d5ca505 to your computer and use it in GitHub Desktop.
Save muathendirangu/d57ef7b086af1747e115eea64d5ca505 to your computer and use it in GitHub Desktop.
The following code shows how to query all rows from the authors table using PDO
<?php
$pdo = require 'connect.php';
$sql = 'SELECT author_id, name
FROM authors';
$statement = $pdo->query($sql);
// get all authors
$authors = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($authors) {
// show the authors
foreach ($authors as $author) {
echo $author['name'] . '<br>';
}
}
// Output sample
Ngugi Wa Thiongo
Caleb Amisi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment