svn: /phpdoc/en/trunk/reference/pdo/ error-handling.xml

From: Date: Fri, 11 Jan 2013 11:09:57 +0000
Subject: svn: /phpdoc/en/trunk/reference/pdo/ error-handling.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-10913@lists.php.net to get a copy of this message
googleguy Fri, 11 Jan 2013 11:09:57 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=329077 Log: Updated PDO error handling chapter with new example and added note to reflect PDO::__construct behavior. This fixes Bug #63845 in that a note is added to inform the user that PDO::__construct will always throw an exception if the connection fails. A new example is also added to reflect that even when the PDO::ATTR_ERRMODE is set in the constructor, and the connection fails, a PDOException will still be thrown. Bug: https://bugs.php.net/63845 (Assigned) Misleading example code Changed paths: U phpdoc/en/trunk/reference/pdo/error-handling.xml Modified: phpdoc/en/trunk/reference/pdo/error-handling.xml =================================================================== --- phpdoc/en/trunk/reference/pdo/error-handling.xml 2013-01-11 02:35:20 UTC (rev 329076) +++ phpdoc/en/trunk/reference/pdo/error-handling.xml 2013-01-11 11:09:57 UTC (rev 329077) @@ -95,6 +95,47 @@ </programlisting> </example> </para> + <note> + <para> + <function>PDO::__construct</function> will always throw a <classname>PDOException</classname> if the connection fails + regardless of which <constant>PDO::ATTR_ERRMODE</constant> is currently set. Uncaught Exceptions are fatal. + </para> + </note> + <para> + <example> + <title>Create a PDO instance and set the error mode from the constructor</title> + <programlisting role="php"> +<![CDATA[ +<?php +$dsn = 'mysql:dbname=test;host=127.0.0.1'; +$user = 'googleguy'; +$password = 'googleguy'; + +/* + Using try/catch around the constructor is still valid even though we set the ERRMODE to WARNING since + PDO::__construct will always throw a PDOException if the connection fails. +*/ +try { + $dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING)); +} catch (PDOException $e) { + echo 'Connection failed: ' . $e->getMessage(); + exit; +} + +// This will cause PDO to throw an error of level E_WARNING instead of an exception (when the table doesn't exist) +$dbh->query("SELECT wrongcolumn FROM wrongtable"); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.wrongtable' doesn't exist in +/tmp/pdo_test.php on line 18 +]]> + </screen> + </example> + </para> </chapter> <!-- Keep this comment at the end of the file

« previous php.doc.cvs (#10913) next »