fetch_style_assoc.php

PHOTO EMBED

Thu Sep 08 2022 09:24:44 GMT+0000 (Coordinated Universal Time)

Saved by @vmap #php #mysql #pdo

<?php

//In this example, we fetch data as an associative array.

$dsn = "mysql:host=localhost;dbname=mydb";
$user = "user12";
$passwd = "12user";

$pdo = new PDO($dsn, $user, $passwd);

$stm = $pdo->query("SELECT * FROM countries");

//In the fetchAll method, we use the PDO::FETCH_ASSOC style.
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $row) {

    printf("{$row['id']} {$row['name']} {$row['population']}\n");
}

?>
content_copyCOPY

https://zetcode.com/php/pdo/