$db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); $users = $db->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC); foreach ($users as $user) { echo $user['name'] . '
'; }
$db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); $orders = $db->query('SELECT * FROM orders')->fetchAll(PDO::FETCH_NUM); $total = 0; foreach ($orders as $order) { $total += $order[3]; } echo 'Total sales: $' . $total;In this example, `fetchAll` retrieves all the orders from the 'orders' table of a MySQL database and stores them in the `$orders` array. The `PDO::FETCH_NUM` parameter specifies that the array should be numeric, meaning the keys are the column indices from the table. Based on the use of `PDO`, this code is using the PHP Data Objects (PDO) package library for database access.