functions instead of mysql_query and mysql_connect for error checking with debug mode and function strSafe for stopping sql inje


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function dbQuery($query, $identifier=''){ // $identifier for better debugging
  2. GLOBAL $debugMode;
  3. $result=mysql_query($query);
  4. if (!$result) {
  5. $message = 'Invalid query $identifier: ' . mysql_error() . "<br />\n";
  6. if($debugMode) $message .= 'Whole query: ' . $query;
  7. die($message);
  8. }
  9. }
  10.  
  11. function dbConnect($user, $pass, $db=FALSE, $host='localhost'){
  12. $link=mysql_connect($host,$user,$pass) or die('Could not connect to database: '.mysql_error());
  13. if(!empty($db)) mysql_select_db($db) or die("Could not select the database $db: ".mysql_error());
  14. return $link;
  15. }
  16.  
  17. function strSafe($str, $char="'"){
  18. return str_replace($char, '\'.$char, $str);
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.