Re: Error Handling

From: Date: Wed, 15 Jun 2005 02:13:21 +0000
Subject: Re: Error Handling
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-19626@lists.php.net to get a copy of this message
* Michael Schreifels <technel@gmail.com>: > I am using PEAR::setErrorHandling() to have PEAR::DB send errors to a > custom error reporting function via the CALLBACK parameter. This is > the function (so far): > > function sql_error($error) { > echo "<div style=\"padding: 5px; background: #f3f3f3; border: 1px > inset #000\">\n"; > echo "\t<h1 style=\"margin: 0 0 20px 0; color: darkred\">SQL " . > ERROR . "</h1>\n"; > echo "\t<p>" . SQL_ERROR_GEN . "</p>\n"; > echo "\t<h2>" . ERROR . "</h2>\n"; > echo "\t<textarea rows=\"6\" cols=\"30\" > name=\"error\">" . > $error->getMessage() . "</textarea>\n\t<h2>Query</h2>\n"; > echo "\t<textarea rows=\"6\" cols=\"30\" > name=\"query\">[QUERY SHOULD > GO HERE]</textarea>\n"; > echo "\t<h2>Current URL</h2>\n\t<input type=\"text\" > name=\"url\" > value=\"{$_SERVER['PHP_SELF']}\" />\n"; > echo "</div>"; > } > > The thing is, I want the query to be displayed where I have "[QUERY > SHOULD GO HERE]" so the person using the script knows exactly what > query caused the error. How do I display the query that triggered the > error? I have tried a function that was like getUserInfo() (or > something of that nature), but it also showed the query (which I > display before the query section in the error function). There are several ways to do this, probably. I use OOP as my programming paradigm, and it comes in handy in this very situation. I typically have a class property of $db that has my connection handle. A PEAR::DB object has a property, $last_query, that stores the last SQL query processed on the handle. Thus, you can reference it in your object as "$this->db->last_query". If you then use an object method as your callback, that's all you need to do to access this information. You may be able to do the same thing procedurally using the $GLOBALS array. If the database handle is defined in the main routine, you may be able to grab it in your function by using something like: if (isset($GLOBALS['db'])) { $db =& $GLOBALS['db']; echo "<h2>Last query:</h2><pre>" . $db->last_query . "</pre>"; } -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association | http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:matthew@garden.org | http://vermontbotanical.org

« previous php.pear.general (#19626) next »