What is PDO?
PDO (PHP Data Objects) is a database abstraction layer that provides a consistent way to interact with different databases (MySQL, PostgreSQL, SQLite, etc.) using the same code.
Wait! Most importantly, PDO is the standard way to prevent SQL Injection attacks.
php
Prepared Statements
Never put user input directly into an SQL query! Use Prepared Statements with placeholders instead.
php
Why use PDO?
- Security: Automatically handles escaping to prevent hacking.
- Flexibility: Switch from MySQL to PostgreSQL by changing just one line (the DSN).
- Error Handling: Can be configured to throw exceptions when a query fails.
php
Fetching Data
fetch(): Gets a single row.fetchAll(): Gets all rows as an array.PDO::FETCH_ASSOC: Returns results as an associative array (the most common mode).
Summary
| Term | Description |
|---|---|
| DSN | Data Source Name (Connection string) |
| Prepare | Sending the query blueprint to the server |
| Execute | Sending the actual data and running the query |
| Bound | Data that is safely attached to a placeholder |
| SQL Inj. | A critical security vulnerability prevented by PDO |
| Best For | Any professional database interaction in PHP |
| Logic | Separate the query logic from the query data |
| Safety | "Trust no user input" |
| Modern | The industry standard replacement for the old mysql_ functions |
| Standard | Part of PHP since 5.1 (2005) |
| Transaction | PDO also supports beginTransaction, commit, and rollBack |