note 81942 deleted from function.mysql-error by nlopess

From: Date: Thu, 20 Mar 2008 12:23:57 +0000
Subject: note 81942 deleted from function.mysql-error by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137232@lists.php.net to get a copy of this message
Note Submitter: Joe at NOSPAM dot brewsterware dot com ---- 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

« previous php.notes (#137232) next »