June 2017
Intermediate to advanced
536 pages
9h 49m
English
Querying for records with PDO is somewhat similar to querying for records with mysqli. We use raw SQL statements in both cases. The difference lies in the convenience of PHP methods and the subtle differences they provide. The following example demonstrates how we can select records from a MySQL table:
<?phptry { $conn = new PDO( "mysql:host=127.0.0.1;dbname=sakila", 'root', 'mL08e!Tq', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] ); $result = $conn->query('SELECT * FROM customer LIMIT 5'); $customers = $result->fetchAll(PDO::FETCH_OBJ); foreach ($customers as $customer) { echo $customer->first_name, ' ', $customer->last_name, PHP_EOL; }} catch (PDOException $e) { echo $e->getMessage(), PHP_EOL;}
This gives the following output: ...
Read now
Unlock full access