PDO Flashcards
What is PDO?
PHP Data Objects - defines a lightweight, consistent interface for accessing databases in PHP.
How are PDO connections established?
By creating instances of the PDO base class.
What happens if your application does not catch the exception thrown from the PDO constructor?
The default action taken by the zend engine is to terminate the script and display a back trace. It’s your responsibility to catch the exception, either explicitly (via a catch statement) or implicitly via set_exception_handler().
How long does a PDO connection remain active?
For the lifetime of the PDO object representing it.
How can you close a PDO connection?
You need to destroy the object by ensuring that all remaining references to it are deleted. You do this by assigning NULL to the variable that holds the object.
What happens if you don’t explicitly close a PDO connection?
PHP will automatically close the connection when the script ends.
How can you use persistent connections with PDO?
Set PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor. If setting this attribute with PDO::setAttribute() after instantiation of the object, the driver will not use persistent connections.
When should you not use persistent connections with PDO?
When you’re using the PDO ODBC driver and your libraries support ODBC Connection Pooling.
What 4 major features do database transactions offer?
Atomicity, Consistency, Isolation, and Durability (ACID).
What does it mean when PDO is running in “auto-commit” mode?
It means that every query you run has its own implicit transaction, if the database supports transactions, or no transaction if the database doesn’t support transactions.
What do you use if you need a transaction in PDO?
PDO::beginTransaction() to initiate a transaction. If the underlying driver does not support transactions, a PDOException will be thrown.
What do you use to finish transactions in PDO?
PDO::commit() or PDO::rollBack.
When will PDO::beginTransaction return true, even though the transaction will fail?
When certain runtime conditions mean that transactions are unavailable, even though they’re supported by the driver. For example, when trying to use transactions on MyISAM tables.
When the script ends or when a connection is about to be closed, if you have an outstanding transaction, what will happen?
PDO will automatically roll it back. This only happens if you initiate the transaction with PDO::beginTransaction. If you manually issue a query that begins a transaction, PDO will not roll it back.
What are prepared statements?
A kind of compiled template for the SQL that an application wants to run, than can be customized using variable parameters.
What are the benefits of prepared statements?
- The query only needs to be parsed and prepared once, but can be executed multiple times.
- The parameters to prepared statements don’t need to be quoted; the driver automatically handles this.
What is the only feature that PDO will emulate for drivers that don’t support it?
Prepared statements.