|
mysqli_error
Returns a string description of the last error
(PHP 5)
Example 1487. Object oriented style<?php Example 1488. Procedural style<?php Code Examples / Notes » mysqli_errorinformation
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 Query: $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. se
The decription "mysqli_error -- Returns a string description of the LAST error" is not exactly that what you get from mysqli_error. You get the error description from the last mysqli-function, not from the last mysql-error. If you have the following situation if (!$mysqli->query("SET a=1")) { $mysqli->query("ROLLBACK;") printf("Errormessage: %s\n", $mysqli->error); } you don't get an error-message, if the ROLLBACK-Query didn't failed, too. In order to get the right error-message you have to write: if (!$mysqli->query("SET a=1")) { printf("Errormessage: %s\n", $mysqli->error); $mysqli->query("ROLLBACK;") } information
Hi, you can also use the new mysqli_sql_exception to catch sql errors. Example: <?php //set up $mysqli_instance here.. $Select = "SELECT xyz FROM mytable "; try { $res = $mysqli_instance->query($Select); }catch (mysqli_sql_exception $e) { print "Error Code ".$e->getCode(); print "Error Message ".$e->getMessage(); print "Strack Trace ".nl2br($e->getTraceAsString()); } ?> Will print out something like Error Code: 0 Error Message No index used in query/prepared statement select sess_value from frame_sessions where sess_name = '5b85upjqkitjsostvs6g9rkul1' Strack Trace: #0 G:\classfiles\lib5\database.php(214): mysqli->query('select sess_val...') #1 G:\classfiles\lib5\Session.php(52): database->getString('select sess_val...') #2 [internal function]: sess_read('5b85upjqkitjsos...') #3 G:\classfiles\includes.php(50): session_start() #4 G:\tests\all_tests.php(4): include('G:\data\WorkSit...') #5 {main} |
Change Languagemysqli_affected_rows mysqli_autocommit mysqli_bind_param mysqli_bind_result mysqli_change_user mysqli_character_set_name mysqli_client_encoding mysqli_close mysqli_commit mysqli_connect_errno mysqli_connect_error mysqli_connect mysqli_data_seek mysqli_debug mysqli_disable_reads_from_master mysqli_disable_rpl_parse mysqli_dump_debug_info mysqli_embedded_server_end mysqli_embedded_server_start mysqli_enable_reads_from_master mysqli_enable_rpl_parse mysqli_errno mysqli_error mysqli_escape_string mysqli_execute mysqli_fetch_array mysqli_fetch_assoc mysqli_fetch_field_direct mysqli_fetch_field mysqli_fetch_fields mysqli_fetch_lengths mysqli_fetch_object mysqli_fetch_row mysqli_fetch mysqli_field_count mysqli_field_seek mysqli_field_tell mysqli_free_result mysqli_get_charset mysqli_get_client_info mysqli_get_client_version mysqli_get_host_info mysqli_get_metadata mysqli_get_proto_info mysqli_get_server_info mysqli_get_server_version mysqli_get_warnings mysqli_info mysqli_init mysqli_insert_id mysqli_kill mysqli_master_query mysqli_more_results mysqli_multi_query mysqli_next_result mysqli_num_fields mysqli_num_rows mysqli_options mysqli_param_count mysqli_ping mysqli_prepare mysqli_query mysqli_real_connect mysqli_real_escape_string mysqli_real_query mysqli_report mysqli_rollback mysqli_rpl_parse_enabled mysqli_rpl_probe mysqli_rpl_query_type mysqli_select_db mysqli_send_long_data mysqli_send_query mysqli_server_end mysqli_server_init mysqli_set_charset mysqli_set_local_infile_default mysqli_set_local_infile_handler mysqli_set_opt mysqli_slave_query mysqli_sqlstate mysqli_ssl_set mysqli_stat mysqli_stmt_affected_rows mysqli_stmt_attr_get mysqli_stmt_attr_set mysqli_stmt_bind_param mysqli_stmt_bind_result mysqli_stmt_close mysqli_stmt_data_seek mysqli_stmt_errno mysqli_stmt_error mysqli_stmt_execute mysqli_stmt_fetch mysqli_stmt_field_count mysqli_stmt_free_result mysqli_stmt_get_warnings mysqli_stmt_init mysqli_stmt_insert_id mysqli_stmt_num_rows mysqli_stmt_param_count mysqli_stmt_prepare mysqli_stmt_reset mysqli_stmt_result_metadata mysqli_stmt_send_long_data mysqli_stmt_sqlstate mysqli_stmt_store_result mysqli_store_result mysqli_thread_id mysqli_thread_safe mysqli_use_result mysqli_warning_count |