note 81942 added to function.mysql-error
| From: | Joe at NOSPAM dot brewsterware dot com | Date: | Thu, 20 Mar 2008 10:38:41 +0000 |
| Subject: | note 81942 added to function.mysql-error | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137229@lists.php.net to get a copy of this message | ||
I written a small function that displays the query and highlights the erroneous part:
<?php
function DisplayQuery( $Query, $ExtraText = '' )
{
$search = array( 'FROM',
'WHERE',
'AND',
'JOIN',
'LEFT <br />JOIN',
'ORDER BY' );
$replace = array( '<br />FROM',
'<br />WHERE',
'<br />AND',
'<br />JOIN',
'<br />LEFT JOIN',
' <br />ORDER BY' );
if ( strlen($ErrorText = @mysql_error()) > 0 )
{
$ErrorNumber = mysql_errno();
$FirstQuotePos = strpos( $ErrorText, "'" );
$LastQuotePos = strpos( $ErrorText, "'", $FirstQuotePos+1 );
$QuerySubString = '';
if ( $FirstQuotePos == $LastQuotePos && false !== $FirstQuotePos && false !==
$LastQuotePos )
{
$QuerySubString = substr( $ErrorText, $FirstQuotePos+1, strlen( $ErrorText ) - $FirstQuotePos - 1
);
} elseif ( $ErrorNumber != 1146 && $FirstQuotePos < $LastQuotePos && false !==
$FirstQuotePos && false !== $LastQuotePos )
{
$QuerySubString = substr( $ErrorText, $FirstQuotePos+1, $LastQuotePos - $FirstQuotePos - 1 );
} elseif ( $ErrorNumber == 1146 && $FirstQuotePos < $LastQuotePos && false !==
$FirstQuotePos && false !== $LastQuotePos )
{
$QuerySubString = substr( $ErrorText, $FirstQuotePos+1, $LastQuotePos - $FirstQuotePos - 1 );
$QuerySubString = substr( $QuerySubString, strpos( $QuerySubString, '.' )+1, strlen(
$QuerySubString ) - strpos( $QuerySubString, '.' ) );
}
$Query = str_replace( $QuerySubString, '<span
style="background:yellow;">' . $QuerySubString . '</span>', $Query
);
}
return '<div style="border: 2px red solid; padding: 4px;">' .
str_replace( $search, $replace, $Query ) . $ExtraText . '</div>';
}
?>
I use it with the following code:
<?php
function exec_sql( $Query, $ShowError = false )
{
if ( !($result = @mysql_query( $Query ) ) )
{
if ( $ShowError )
{
echo DisplayQuery( $Query, '<br /><br />' . mysql_error() );
}
}
return $result;
}
?>
If an erroneous query is passed to exec_sql with the $ShowError parameter set to true, the query
will be displayed with the erroneous part highlighted.
Hope someone finds this useful.
Joe
http://www.brewsterware.com
----
Server IP: 92.48.74.199
Probable Submitter: 89.241.255.91
----
Manual Page -- http://www.php.net/manual/en/function.mysql-error.php
Edit -- https://master.php.net/note/edit/81942
Del: integrated -- https://master.php.net/note/delete/81942/integrated
Del: useless -- https://master.php.net/note/delete/81942/useless
Del: bad code -- https://master.php.net/note/delete/81942/bad+code
Del: spam -- https://master.php.net/note/delete/81942/spam
Del: non-english -- https://master.php.net/note/delete/81942/non-english
Del: in docs -- https://master.php.net/note/delete/81942/in+docs
Del: other reasons-- https://master.php.net/note/delete/81942
Reject -- https://master.php.net/note/reject/81942
Search -- https://master.php.net/manage/user-notes.php