note 68077 added to function.mysql-error

From: Date: Wed, 12 Jul 2006 12:38:21 +0000
Subject: note 68077 added to function.mysql-error
Groups: php.notes 
Request: Send a blank email to php-notes+get-114693@lists.php.net to get a copy of this message
When creating large applications it's quite handy to create a custom function for handling queries. Just include this function in every script. And use db_query(in this example) instead of mysql_query. This example prompts an error in debugmode (variable $b_debugmode ). An e-mail with the error will be sent to the site operator otherwise. The script writes a log file in directory ( in this case /log ) as well. The system is vulnerable when database/query information is prompted to visitors. So be sure to hide this information for visitors anytime. Regars, Lennart Poot http://www.twing.nl <?php $b_debugmode = 1; // 0 || 1 $system_operator_mail = 'developer@company.com'; $system_from_mail = 'info@mywebsite.com'; function db_query( $query ){ global $b_debugmode; // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { if($b_debugmode){ $message = '<b>Invalid query:</b><br>' . mysql_error() . '<br><br>'; $message .= '<b>Whole query:</b><br>' . $query . '<br><br>'; die($message); } raise_error('db_query_error: ' . $message); } return $result; } function raise_error( $message ){ global $system_operator_mail, $system_from_mail; $serror= "Env: " . $_SERVER['SERVER_NAME'] . "\r\n" . "timestamp: " . Date('m/d/Y H:i:s') . "\r\n" . "script: " . $_SERVER['PHP_SELF'] . "\r\n" . "error: " . $message ."\r\n\r\n"; // open a log file and write error $fhandle = fopen( '/logs/errors'.date('Ymd').'.txt', 'a' ); if($fhandle){ fwrite( $fhandle, $serror ); fclose(( $fhandle )); } // e-mail error to system operator if(!$b_debugmode) mail($system_operator_mail, 'error: '.$message, $serror, 'From: ' . $system_from_mail ); } ?> ---- Server IP: 194.109.193.119 Probable Submitter: 213.93.122.179 ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-error.php Edit -- http://master.php.net/note/edit/68077 Del: integrated -- http://master.php.net/note/delete/68077/integrated Del: useless -- http://master.php.net/note/delete/68077/useless Del: bad code -- http://master.php.net/note/delete/68077/bad+code Del: spam -- http://master.php.net/note/delete/68077/spam Del: non-english -- http://master.php.net/note/delete/68077/non-english Del: in docs -- http://master.php.net/note/delete/68077/in+docs Del: other reasons-- http://master.php.net/note/delete/68077 Reject -- http://master.php.net/note/reject/68077 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114693) next »