PDO Flashcards

1
Q

What is PDO?

A

PHP Data Objects - defines a lightweight, consistent interface for accessing databases in PHP.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are PDO connections established?

A

By creating instances of the PDO base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens if your application does not catch the exception thrown from the PDO constructor?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How long does a PDO connection remain active?

A

For the lifetime of the PDO object representing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you close a PDO connection?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if you don’t explicitly close a PDO connection?

A

PHP will automatically close the connection when the script ends.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you use persistent connections with PDO?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When should you not use persistent connections with PDO?

A

When you’re using the PDO ODBC driver and your libraries support ODBC Connection Pooling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What 4 major features do database transactions offer?

A

Atomicity, Consistency, Isolation, and Durability (ACID).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does it mean when PDO is running in “auto-commit” mode?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do you use if you need a transaction in PDO?

A

PDO::beginTransaction() to initiate a transaction. If the underlying driver does not support transactions, a PDOException will be thrown.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do you use to finish transactions in PDO?

A

PDO::commit() or PDO::rollBack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When will PDO::beginTransaction return true, even though the transaction will fail?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When the script ends or when a connection is about to be closed, if you have an outstanding transaction, what will happen?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are prepared statements?

A

A kind of compiled template for the SQL that an application wants to run, than can be customized using variable parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the benefits of prepared statements?

A
  1. The query only needs to be parsed and prepared once, but can be executed multiple times.
  2. The parameters to prepared statements don’t need to be quoted; the driver automatically handles this.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the only feature that PDO will emulate for drivers that don’t support it?

A

Prepared statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are stored procedures?

A

Subroutines stored in the database catalog, that can be called and executed by applications.

19
Q

What is PDO::ERRORMODE_SILENT?

A

This is the default PDO error mode. PDO will set the error code for you to inspect in PDO::errorCode() and PDO::errorInfo() methods on both the statement and database objects. If the error resulted from a call on a statement object, you would invoke the PDOStatement::errorCode() or PDOStatement::errorInfo() method on that object. If the error resulted from a call on the database object, you would invoke those methods on the database object instead.

20
Q

What are the three PDO error modes?

A

PDO::ERRORMODE_SILENT, PDO::ERRORMODE_WARNING, PDO::ERRORMODE_EXCEPTION.

21
Q

What is PDO::ERRORMODE_WARNING?

A

In addition to setting the error code, as for PDO::ERRORMODE_SILENT, it emits a traditional E_WARNING message. This is useful during debugging/testing.

22
Q

What is PDO::ERRORMODE_EXCEPTION?

A

In addition to setting the error code, as for PDO::ERRORMODE_SILENT, PDO will throw a PDOException and set its properties to reflect the error code and error information.

23
Q

What does PDO::errorCode() return?

A

A single SQLSTATE code.

24
Q

What does PDO::errorInfo() return?

A

An array containing the SQLSTATE code, the driver specific error code and driver specific error string.

25
Q

When does a PDOException get thrown, regardless of error mode?

A

When PDO::__construct() fails to make a connection. Uncaught exceptions are fatal.

26
Q

How do you work with blobs in PDO?

A

Use the PDO::PARAM_LOB type code in your PDOStatement::bindParam() or PDOStatement::bindColumn() calls.

27
Q

What does PDO::PARAM_LOB do?

A

It tells PDO to map the data as a stream, so that you can manipulate it using the PHP Streams API.

28
Q

What exactly does the PDO class represent?

A

A connection between PHP and a database server.

29
Q

What exactly does the PDOStatement class represent?

A

A prepared statement and, after the statement is executed, an associated result set.

30
Q

What’s the difference between a database driver and a database connector?

A

In MySQL documentation, the term “driver” is reserved for software that provides the database-specific part of a connector package.

31
Q

What 3 different APIs does PHP offer to connect to MySQL?

A

mysql, mysqli, and PDO

32
Q

Why shouldn’t you use the old mysql extension for new development?

A

It’s deprecated in PHP 5.5.0, and will be removed in the future.

33
Q

Which is the best-performing PHP MySQL extension?

A

The overall performance of all 3 is considered to be about the same.

34
Q

Which compile-time library for MySQL is recommended?

A

mysqlnd, and not libmysqlclient

35
Q

Do MySQL queries use buffered or unbuffered mode by default?

A

Buffered.

36
Q

What is MySQL query buffered mode?

A

Query results are immediately transferred from the MySQL server to PHP, and is then kept in the memory of the PHP process.

37
Q

What are the advantages of buffered queries?

A

It allows additional operations like counting the number of rows, moving (seeking) the current result pointer, and issuing further queries on the same connection while working on the result set.

38
Q

What is the downside of buffered queries?

A

Larger result sets might require quite a lot of memory.

39
Q

Regarding result set memory, what is the difference between using libmysqlclient vs. mysqlnd?

A

When using libmysqlclient as a library, PHP’s memory limit won’t count the memory used for result sets unless the data is fetched into PHP variables. With mysqlnd, the memory accounted for will include the full result set.

40
Q

When should buffered queries be used?

A

When you expect only a limited result set, or need to know the amount of returned rows before reading all rows.

41
Q

When should unbuffered queries be used?

A

When you expect larger results.

42
Q

Can you set a MySQL character set at runtime?

A

Yes.

43
Q

Will the escaping mechanisms for the MySQL APIs use character sets defined within a query?

A

No. Each API’s charset altering method should be used.