PDO in PHP Example
The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. SO, in this article, we will learn PDO in PHP with a simple Example. Each database driver that implements the PDO interface can expose database-specific features as a regular extension function. Note that it is not possible to perform database functions using the PDO extension by itself; a database-specific PDO driver is to be used to access a database server.
PDO provides a data-access abstraction layer, which means that, regardless of which database we’re using, we can use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn’t rewrite SQL or emulate missing features.
MYSQLi and SQLite extensions have their own way of accessing databases. They have different methods to do their work; as a result, switching from one database system to another essentially involves rewriting all the database code to use the new methods.
Advantages of PDO:
- Supports multiple databases like SQL server, PostgreSQL, MySQL, etc.
- Exception handling support
- Uses prepare -> execute a model for database modification.
Uses prepared statements to prevent SQL injection.
PDO classes:
- PDO – It represents a connection between PHP and the databases.
- PDO Statement – prepared statement and after executing the statement, sets the corresponding result.
- PDO Exception – It represents errors raised by PDO.