Guided Notes
Databases with PDO
Connecting with PDO, prepared statements, CRUD operations, fetching results, transactions, and preventing SQL injection.
PHP and databases
Slide 1 / 5Most PHP applications store data in a relational database — usually MySQL or MariaDB, also PostgreSQL and SQLite. PHP talks to them through: • PDO (PHP Data Objects) — one API for many databases; recommended, • mysqli — MySQL-specific.
Connecting to MySQL: $pdo = new PDO('mysql:host=localhost;dbname=shop;charset=utf8mb4', $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); This course's labs use SQLite in memory — the same PDO code.
