Posted By

brownrl on 06/24/09


Tagged

database wrapper query


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

SmpleJohn
jseltzer
vali29
umang_nine


PHP dbquery


 / 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.

  1. // function dbquery()
  2. // Call a database query
  3. //
  4. // The purpose of this function is to wrap a dbquery into function
  5. //
  6. // You do this for two reasons,
  7. //
  8. // 1) Make it so that if you db connection, object, etc... changes you won't be screwed.
  9. // 2) Make Error reporting more attractive and maybe useful.
  10. //
  11. // $q -> the query you want to run
  12. //
  13.  
  14. function dbquery( $q )
  15. {
  16. // Lets get the Databas ADO db lite object
  17. // you probably need to change this code and do something different
  18. //
  19. // Actually, that whole freaking point of having this function
  20. // wrap the database query so that if it changes you won't be screwed.
  21. // Regardless!
  22.  
  23. // we need the connection
  24. global $gCms;
  25. $dbc =& $gCms->GetDb();
  26.  
  27. // Execute!
  28. $qr = $dbc->Execute( $q );
  29.  
  30. // if all goes well then return the record set other wise...
  31. if( $qr )
  32. {
  33. return $qr;
  34. }
  35.  
  36. // something went wrong
  37. else
  38. {
  39. // lets build a small backtrace and send it out so we know where this ocured
  40. $debug = debug_backtrace();
  41.  
  42. $mesg = "";
  43.  
  44. //better error mesg
  45. $mesg .= "<div style=\"color: #000000; background-color: #FFFFFF; position: absolute; top: 0px; left: 0px; width: 640px; height: 480px; overflow: auto;\"><pre>\n";
  46.  
  47. $mesg .= "<b>DataBase Error</b>\n";
  48. $mesg .= "There has been an error in the database statement.\n\n";
  49. $mesg .= "<b>SQL</b>\n";
  50. $mesg .= "<i>".$q."</i>\n\n";
  51.  
  52. $mesg .= "<b>File/Line Stack</b>\n\n";
  53.  
  54. foreach( $debug as $k => $v )
  55. {
  56. $mesg .= "FILE: ".$v['file']."\n";
  57. $mesg .= "LINE: ".$v['line']."\n\n";
  58. }
  59.  
  60. $mesg .= "-------------------------------------------------------------\n";
  61.  
  62. // what is mysql saying?
  63. $mesg .= mysql_error() ."\n";
  64. $mesg .= "-------------------------------------------------------------\n\n";
  65.  
  66. $mesg .= "This error has been sent on the development team.\n";
  67. $mesg .= "There is no reason to call or email.\n";
  68. $mesg .= "</pre></div>\n";
  69.  
  70. // we could also send the message to your email address here.
  71. // or you could do what I do and just wait for them to email you anyway
  72. // cause you know god forbid if users actually read what is on the screen
  73. // stupid users: //{}\\
  74.  
  75. // die if there was a db error
  76. // if there is an error in database calling I really don't want my
  77. // application going further...
  78. die( $mesg );
  79. }
  80. }

Report this snippet  

You need to login to post a comment.