/ Published in: PHP
URL: http://www.itsgotto.be/cv.php
Here is a wrapper for making database queries. It doesn't matter who the F!@k you think you are you should always wrap querying the database in your own function.
WHY?
Because when you take your application to a new server, you won't have to change all the calls to mysqlquery() to pgsql_query() or $db->query() or what ever you are now using.
!!!Plus!!!
When you query fails you can handle that in a very nice way with a proper error screen and maybe send an email to your self.
Expand |
Embed | Plain Text
// function dbquery() // Call a database query // // The purpose of this function is to wrap a dbquery into function // // You do this for two reasons, // // 1) Make it so that if you db connection, object, etc... changes you won't be screwed. // 2) Make Error reporting more attractive and maybe useful. // // $q -> the query you want to run // function dbquery( $q ) { // Lets get the Databas ADO db lite object // you probably need to change this code and do something different // // Actually, that whole freaking point of having this function // wrap the database query so that if it changes you won't be screwed. // Regardless! // we need the connection global $gCms; $dbc =& $gCms->GetDb(); // Execute! $qr = $dbc->Execute( $q ); // if all goes well then return the record set other wise... if( $qr ) { return $qr; } // something went wrong else { // lets build a small backtrace and send it out so we know where this ocured $mesg = ""; //better error mesg $mesg .= "<div style=\"color: #000000; background-color: #FFFFFF; position: absolute; top: 0px; left: 0px; width: 640px; height: 480px; overflow: auto;\"><pre>\n"; $mesg .= "<b>DataBase Error</b>\n"; $mesg .= "There has been an error in the database statement.\n\n"; $mesg .= "<b>SQL</b>\n"; $mesg .= "<i>".$q."</i>\n\n"; $mesg .= "<b>File/Line Stack</b>\n\n"; foreach( $debug as $k => $v ) { $mesg .= "FILE: ".$v['file']."\n"; $mesg .= "LINE: ".$v['line']."\n\n"; } $mesg .= "-------------------------------------------------------------\n"; // what is mysql saying? $mesg .= "-------------------------------------------------------------\n\n"; $mesg .= "This error has been sent on the development team.\n"; $mesg .= "There is no reason to call or email.\n"; $mesg .= "</pre></div>\n"; // we could also send the message to your email address here. // or you could do what I do and just wait for them to email you anyway // cause you know god forbid if users actually read what is on the screen // stupid users: //{}\\ // die if there was a db error // if there is an error in database calling I really don't want my // application going further... } }
You need to login to post a comment.
