11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 31 • INTRODUCING PDOwww.it-ebooks.infoThe alias can subsequently be called by the PDO constructor like so:$dbh = new PDO('mysqlpdo', 'webuser', 'secret');Unlike the previous method, this method doesn’t allow for the username and password to beincluded in the DSN.Using PDO’s Connection-Related OptionsThere are several connection-related options for PDO that you might consider tweaking by passing theminto the driver_opts array. These options are enumerated here:• PDO::ATTR_AUTOCOMMIT: This option determines whether PDO will commit eachquery as it’s executed, or will wait for the commit() method to be executed beforeeffecting the changes.• PDO::ATTR_CASE: You can force PDO to convert the retrieved column charactercasing to all uppercase, to convert it to all lowercase, or to use the columns exactlyas they’re found in the database. Such control is accomplished by setting thisoption to one of three values: PDO::CASE_UPPER, PDO::CASE_LOWER, orPDO::CASE_NATURAL, respectively.• PDO::ATTR_EMULATE_PREPARES: Enabling this option makes it possible for preparedstatements to take advantage of <strong>MySQL</strong>’s query cache.• PDO::ATTR_ERRMODE: PDO supports three error-reporting modes,PDO::ERRMODE_EXCEPTION, PDO::ERRMODE_SILENT, and PDO::ERRMODE_WARNING. Thesemodes determine what circumstances cause PDO to report an error. Set thisoption to one of these three values to change the default behavior, which isPDO::ERRMODE_EXCEPTION. This feature is discussed in further detail in the latersection “Handling Errors.”• PDO::ATTR_ORACLE_NULLS: When set to TRUE, this attribute causes empty strings tobe converted to NULL when retrieved. By default this is set to FALSE.• PDO::ATTR_PERSISTENT: This option determines whether the connection ispersistent. By default this is set to FALSE.• PDO::ATTR_PREFETCH: Prefetching is a database feature that retrieves several rowseven if the client is requesting one row at a time, the reasoning being that if theclient requests one row, he’s likely going to want others. Doing so decreases thenumber of database requests and therefore increases efficiency. This option setsthe prefetch size, in kilobytes, for drivers that support this feature.• PDO::ATTR_TIMEOUT: This option sets the number of seconds to wait before timingout. <strong>MySQL</strong> currently does not support this option.• PDO::DEFAULT_FETCH_MODE: You can use this option to set the default fetching mode(associative arrays, indexed arrays, or objects), thereby saving some typing if youconsistently prefer one particular method.612

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!