note 103724 added to mysqli.query
| From: | michaelnatassociationsplusdotsee at osu1 dot php dot net | Date: | Fri, 29 Apr 2011 18:51:52 +0000 |
| Subject: | note 103724 added to mysqli.query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179288@lists.php.net to get a copy of this message | ||
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' );
----
Server IP: 69.147.83.197
Probable Submitter: 96.53.16.122
----
Manual Page -- http://www.php.net/manual/en/mysqli.query.php
Edit -- https://master.php.net/note/edit/103724
Del: integrated -- https://master.php.net/note/delete/103724/integrated
Del: useless -- https://master.php.net/note/delete/103724/useless
Del: bad code -- https://master.php.net/note/delete/103724/bad+code
Del: spam -- https://master.php.net/note/delete/103724/spam
Del: non-english -- https://master.php.net/note/delete/103724/non-english
Del: in docs -- https://master.php.net/note/delete/103724/in+docs
Del: other reasons-- https://master.php.net/note/delete/103724
Reject -- https://master.php.net/note/reject/103724
Search -- https://master.php.net/manage/user-notes.php