note 103724 deleted from mysqli.query by danbrown

From: Date: Fri, 29 Apr 2011 19:35:55 +0000
Subject: note 103724 deleted from mysqli.query by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179289@lists.php.net to get a copy of this message
Note Submitter: michaeln at associationsplus dot see, eh ---- If you ever want to write production quality code but for whatever reason do not want to use libraries like PDO or ADODB which have built in query error handling, consider extending the MySQLi class yourself and overloading or offering alternatives for function such as real_escape_string() and query(). For example, to make a simple addition which allows logging query errors to the log file and notifies someone via email consider something like this: class mysqliplus extends mysqli{ public function __construct( $host = '', $username = '', $passwd = '', $dbname = '', $port = '', $socket = '' ){ if( $host === '' ) $host = ini_get('mysqli.default_host'); if( $username === '' ) $username = ini_get('mysqli.default_user'); if( $passwd === '' ) $passwd = ini_get('mysqli.default_pw'); if( $port === '' ) $port = ini_get('mysqli.default_port'); if( $socket === '' ) $socket = ini_get('mysqli.default_socket'); return parent::__construct( $host, $username, $passwd, $dbname, $port, $socket ); } function esc( $input, $like_wildcards = false ){ if( $like_wildcards ) $input = str_replace( array( '%', '_' ), array( '\%', '\_' ), $input ); return $this->real_escape_string( $input ); } //NOTE: in PHP there is also a $resultmode parameter as well, you can add that if you //like, but we do not use it so we left it out function query( $query, $file, $line, $user = '', $showError = 0, $die = 1, $sendEmail = 1 ){ $query = str_replace( "\t", ' ', $query ); $query = str_replace( ' ', ' ', $query ); $result = parent::query( $query ); if( $result === false ){ $sql_add = ' <tr> <td style="font: 10pt Arial" valign="top"><b>Query: </b></td> <td><pre>'.$query.'</pre></td> </tr> <tr> <td style="font: 10pt Arial" valign="top"><b>MySQL Error: </b></td> <td style="font: 10pt Arial">Error # '.$this->errorno.' - '.$this->error.'</td> </tr> '; $error_subj = 'Query Error'; $errorMessage = ' <span style="font: 10pt Arial">An error has occurred. </span><br><br> <table> '.$sql_add.' <tr> <td style="font: 10pt Arial" valign="top"><b>Script Filename: </b></td> <td style="font: 10pt Arial">'.$_SERVER['SCRIPT_FILENAME'].'</td> </tr> <tr> <td style="font: 10pt Arial" valign="top"><b>File: </b></td> <td style="font: 10pt Arial">'.$file.'</td> </tr> <tr> <td style="font: 10pt Arial" valign="top"><b>User (If Applicable): </b></td> <td style="font: 10pt Arial">'.$user.'</td> </tr> <tr> <td style="font: 10pt Arial" valign="top"><b>Line: </b></td> <td style="font: 10pt Arial">'.$line.'</td> </tr> </table> '; if( $sendEmail ){ //email $errorMessage with whichever email function you use here, such as use mail() or PHPMailer if( $showError > 0 ) echo $errorMessage; elseif( $showError == 0) echo "<p>We are currently experiencing technical difficulties. This error has been logged. Please try again later.</p>"; else{ ; } // less than 0 means be silent error_log( 'mysqli2: "' .str_replace( "\n", ' ', str_replace( "\t", ' ', str_replace( "\r", ' ', $query ) ) ) .'" in '.$file.':'.$line.' called from '.$_SERVER['SCRIPT_FILENAME'], 0 ); if( $die == true ){ $this->close(); die(); } } return $result; } } Test: require_once( 'mysqliplus.class.php' ); $link = new mysqliplus( /*your login info here*/ ); $link ->query( 'SELECT something FROM somewhere WHERE value LIKE "%'.$link->esc( $_POST['some_post_val'], 1 ).'%"', __FILE__, __LINE__, 'progammer_username_or_mysql_connection_user' );

« previous php.notes (#179289) next »