note 103423 added to class.pdoexception

From: Date: Tue, 12 Apr 2011 14:31:36 +0000
Subject: note 103423 added to class.pdoexception
Groups: php.notes 
Request: Send a blank email to php-notes+get-178772@lists.php.net to get a copy of this message
An extended Exception similar to the one from samuelelliot+php dot net at gmail dot com, but the SQLSTATE is saved in its own property. <?php class XtPDOException extends PDOException { protected $sqlstate = ""; public function __construct(PDOException $e) { parent::__construct($e->getMessage(), $e->getCode(), $e); // check for an SQLSTATE $hasSQLState = (false !== strpos($e->getMessage(), "SQLSTATE[")); // set sqlstate property if ($hasSQLState) { $re = '#SQLSTATE\[(\w+)\]#'; if (preg_match($re, $e->getMessage(), $matches)) { $this->sqlstate = $matches[1]; } } // set message from errorInfo propery if ($e->errorInfo) { $this->message = $e->errorInfo[2]; } // or try cutting it from the original message elseif ($hasSQLState) { $re = '#SQLSTATE\[\w+\](?: \[\w+\]|:) (.*)#'; if (preg_match($re, $e->getMessage(), $matches)) { $this->message = $matches[1]; } } } public function getSQLState() { return $this->sqlstate; } } ?> usage example: <?php try { $pdo = new PDO($dns, $username, $password, $options); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->query('bla bla blubb'); // ... } catch (PDOException $e) { throw new XtPDOException($e); } ?> note: when you’re using the XDebug extension, you have a ready-to-use HTML output table in Exception->xdebug_message. ---- Server IP: 69.147.83.197 Probable Submitter: 84.183.15.226 ---- Manual Page -- http://www.php.net/manual/en/class.pdoexception.php Edit -- https://master.php.net/note/edit/103423 Del: integrated -- https://master.php.net/note/delete/103423/integrated Del: useless -- https://master.php.net/note/delete/103423/useless Del: bad code -- https://master.php.net/note/delete/103423/bad+code Del: spam -- https://master.php.net/note/delete/103423/spam Del: non-english -- https://master.php.net/note/delete/103423/non-english Del: in docs -- https://master.php.net/note/delete/103423/in+docs Del: other reasons-- https://master.php.net/note/delete/103423 Reject -- https://master.php.net/note/reject/103423 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178772) next »