Mysqli error handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Okay, so I've finally got an opportunity to start converting our code to
use Mysqli instead of the old mysql_* functions. Mysqli is new to me, but
I thought things were going well until the first time I tried to run a
query with a syntax error in it, and it threw up a PHP warning. Being that
Mysqli is an OO API, I guess I assumed that any errors would simply
surface as exceptions, and indeed, I got an exception in this case. But I
also got a PHP warning containing the same information.

This is the first thing that strikes me as terribly wrong. I think any
API, OO or not, should never spit out errors/warnings/notices for routine
errors (like, in this case, an SQL syntax error). Rather, the
function/method should signal an error through a defined mechanism, such
as the return value (e.g., false on failure), or via an exception for
class methods. This philosophy jives well with my understand of the
direction of error handling in PHP, which is to use exceptions, rather
than errors/warnings/notices, for all the newer OOP APIs.

Secondly, I can't find a good way to handle these warnings. We do all
development with error handling set to E_STRICT | E_ALL, and our general
policy is that no such errors should be generated. This behavior by Mysqli
means that we effectively need to catch SQL problems in both our error
handlers and in our exception handling. I recognize that we could just use
suppression, but that's, well, nasty.

Digging a bit more, I found mysqli_report(). This looks pretty good, but I
find it odd that 1) it's a procedural-only function that controls an OO
API, and 2), it takes affect for the entire request, which effectively
means it has to be set up right for each database access rather than just
once per database instance. I suspect this relates to the underlying
implementation, but it's still quite the letdown.

Is there something I'm missing in all this, or is really as clunky as it
seems? What have those of you with more Mysqli experience found to work
well as a good error-handling pattern when working with it?


--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/







Notice: This communication, including attachments, may contain information that is confidential. It constitutes non-public information intended to be conveyed only to the designated recipient(s). If the reader or recipient of this communication is not the intended recipient, an employee or agent of the intended recipient who is responsible for delivering it to the intended recipient, or if you believe that you have received this communication in error, please notify the sender immediately by return e-mail and promptly delete this e-mail, including attachments without reading or saving them in any manner. The unauthorized use, dissemination, distribution, or reproduction of this e-mail, including attachments, is prohibited and may be unlawful. If you have received this email in error, please notify us immediately by e-mail or telephone and delete the e-mail and the attachments (if any).

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux