note 61044 added to function.mysqli-error
| From: | information at saunderswebsolutions dot com | Date: | Tue, 24 Jan 2006 10:37:50 +0000 |
| Subject: | note 61044 added to function.mysqli-error | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102345@lists.php.net to get a copy of this message | ||
The mysqli_sql_exception class is not available to PHP 5.05
I used this code to catch errors
<?php
$query = "SELECT XXname FROM customer_table ";
$res = $mysqli->query($query);
if (!$res) {
printf("Errormessage: %s\n", $mysqli->error);
}
?>
The problem with this is that valid values for $res are: a mysqli_result object , true or false
This doesn't tell us that there has been an error with the sql used.
If you pass an update statement, false is a valid result if the update fails.
So, a better way is:
<?php
$query = "SELECT XXname FROM customer_table ";
$res = $mysqli->query($query);
if (!$mysqli->error) {
printf("Errormessage: %s\n", $mysqli->error);
}
?>
This would output something like:
Unexpected PHP error [mysqli::query() [<a
href='function.query'>function.query</a>]: (42S22/1054): Unknown column
'XXname' in 'field list'] severity [E_WARNING] in [G:\database.php] line [249]
Very frustrating as I wanted to also catch the sql error and print out the stack trace.
A better way is:
<?php
mysqli_report(MYSQLI_REPORT_OFF); //Turn off irritating default messages
$mysqli = new mysqli("localhost", "my_user", "my_password",
"world");
$query = "SELECT XXname FROM customer_table ";
$res = $mysqli->query($query);
if ($mysqli->error) {
try {
throw new Exception("MySQL error $mysqli->error <br> Query:<br> $query",
$msqli->errno);
} catch(Exception $e ) {
echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br
>";
echo nl2br($e->getTraceAsString());
}
}
//Do stuff with the result
?>
Prints out something like:
Error No: 1054
Unknown column 'XXname' in 'field list'
Query:
SELECT XXname FROM customer_table
#0 G:\\database.php(251): database->dbError('Unknown column ...', 1054,
'getQuery()', 'SELECT XXname F...')
#1 G:\data\WorkSites\1framework5\tests\dbtest.php(29): database->getString('SELECT XXname
F...')
#2 c:\PHP\includes\simpletest\runner.php(58): testOfDB->testGetVal()
#3 c:\PHP\includes\simpletest\runner.php(96): SimpleInvoker->invoke('testGetVal')
#4 c:\PHP\includes\simpletest\runner.php(125):
SimpleInvokerDecorator->invoke('testGetVal')
#5 c:\PHP\includes\simpletest\runner.php(183):
SimpleErrorTrappingInvoker->invoke('testGetVal')
#6 c:\PHP\includes\simpletest\simple_test.php(90): SimpleRunner->run()
#7 c:\PHP\includes\simpletest\simple_test.php(498): SimpleTestCase->run(Object(HtmlReporter))
#8 c:\PHP\includes\simpletest\simple_test.php(500): GroupTest->run(Object(HtmlReporter))
#9 G:\all_tests.php(16): GroupTest->run(Object(HtmlReporter))
This will actually print out the error, a stack trace and the offending sql statement. Much more
helpful when the sql statement is generated somewhere else in the code.
----
Server IP: 85.116.0.130
Probable Submitter: 81.1.88.65
----
Manual Page -- http://www.php.net/manual/en/function.mysqli-error.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61044
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61044&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61044&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61044&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61044&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61044&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61044&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61044&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61044&report=yes
Search -- http://master.php.net/manage/user-notes.php