|
mysql_error
Returns the text of the error message from previous MySQL operation
(PHP 4, PHP 5, PECL mysql:1.0)
Example 1424. mysql_error() example<?php The above example will output something similar to: 1049: Unknown database 'nonexistentdb' Related Examples ( Source code ) » mysql_error Examples ( Source code ) » Delete data from database by ID Examples ( Source code ) » Use form to add data to database Examples ( Source code ) » Adding a Row to a Table Examples ( Source code ) » List Database, Table, and Field Examples ( Source code ) » Listing All Rows and Fields in a Table Examples ( Source code ) » Get data from database query Examples ( Source code ) » Finding the Number of Rows Returned by a SELECT Statement with mysql_num_rows() Examples ( Source code ) » Connect to MySQL Examples ( Source code ) » Opening a Connection and Selecting a Database Examples ( Source code ) » Table-Creation Script Examples ( Source code ) » Create table and install data Examples ( Source code ) » Listing Tables in a Database Code Examples / Notes » mysql_errorolaf
When dealing with user input, make sure that you use <?php echo htmlspecialchars (mysql_error ()); ?> instead of <?php echo mysql_error (); ?> Otherwise it might be possible to crack into your system by submitting data that causes the SQL query to fail and that also contains javascript commands. Would it make sense to change the examples in the documentation for mysql_query () and for mysql_error () accordingly? l dot poot
When creating large applications it's quite handy to create a custom function for handling queries. Just include this function in every script. And use db_query(in this example) instead of mysql_query. This example prompts an error in debugmode (variable $b_debugmode ). An e-mail with the error will be sent to the site operator otherwise. The script writes a log file in directory ( in this case /log ) as well. The system is vulnerable when database/query information is prompted to visitors. So be sure to hide this information for visitors anytime. Regars, Lennart Poot http://www.twing.nl <?php $b_debugmode = 1; // 0 || 1 $system_operator_mail = 'developer@company.com'; $system_from_mail = 'info@mywebsite.com'; function db_query( $query ){ global $b_debugmode; // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { if($b_debugmode){ $message = '<b>Invalid query:</b> ' . mysql_error() . ' '; $message .= '<b>Whole query:</b> ' . $query . ' '; die($message); } raise_error('db_query_error: ' . $message); } return $result; } function raise_error( $message ){ global $system_operator_mail, $system_from_mail; $serror= "Env: " . $_SERVER['SERVER_NAME'] . "\r\n" . "timestamp: " . Date('m/d/Y H:i:s') . "\r\n" . "script: " . $_SERVER['PHP_SELF'] . "\r\n" . "error: " . $message ."\r\n\r\n"; // open a log file and write error $fhandle = fopen( '/logs/errors'.date('Ymd').'.txt', 'a' ); if($fhandle){ fwrite( $fhandle, $serror ); fclose(( $fhandle )); } // e-mail error to system operator if(!$b_debugmode) mail($system_operator_mail, 'error: '.$message, $serror, 'From: ' . $system_from_mail ); } ?> phpnet
This is a big one - As of MySQL 4.1 and above, apparently, the way passwords are hashed has changed. PHP 4.x is not compatible with this change, though PHP 5.0 is. I'm still using the 4.x series for various compatibility reasons, so when I set up MySQL 5.0.x on IIS 6.0 running PHP 4.4.4 I was surpised to get this error from mysql_error(): MYSQL: Client does not support authentication protocol requested by server; consider upgrading MySQL client According to the MySQL site (http://dev.mysql.com/doc/refman/5.0/en/old-client.html) the best fix for this is to use the OLD_PASSWORD() function for your mysql DB user. You can reset it by issuing to MySQL: Set PASSWORD for 'user'@'host' = OLD_PASSWORD('password'); This saved my hide. 10-may-2001 03:03
some error can't handle. Example: ERROR 1044: Access denied for user: 'ituser@mail.ramon.intranet' to database 'itcom' This error ocurrs when a intent of a sql insert of no authorized user. The results: mysql_errno = 0 and the mysql_error = "" . josh ><>
Oops, the code in my previous post only works for queries that don't return data (INSERT, UPDATE, DELETE, etc.), this updated function should work for all types of queries (using $result = myquery($query);): function myquery ($query) { $result = mysql_query($query); if (mysql_errno()) echo "MySQL error ".mysql_errno().": ".mysql_error()."\n When executing: \n$query\n "; return $result; } 05-jan-2005 08:53
My suggested implementation of mysql_error(): $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); This will print out something like... <b>A fatal MySQL error occured</b>. Query: SELECT * FROM table Error: (err_no) Bla bla bla, you did everything wrong It's very useful to see your query in order to detect problems with syntax. Most often, the output message from MySQL doesn't let you see enough of the query in the error message to let you see where your query went bad- it a missing quote, comma, or ( or ) could have occured well before the error was detected. I do -not- recomend using this procedure, however, for queries which execute on your site that are not user-specific as it has the potential to leak sensative data. Recomended use is just for debugging/building a script, and for general user-specific queries which would at the worst, leak the users own information to themself. Good luck, -Scott scott
My suggested implementation of mysql_error(): $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); This will print out something like... <b>A fatal MySQL error occured</b>. Query: SELECT * FROM table Error: (err_no) Bla bla bla, you did everything wrong It's very useful to see your query in order to detect problems with syntax. Most often, the output message from MySQL doesn't let you see enough of the query in the error message to let you see where your query went bad- it a missing quote, comma, or ( or ) could have occured well before the error was detected. I do -not- recomend using this procedure, however, for queries which execute on your site that are not user-specific as it has the potential to leak sensative data. Recomended use is just for debugging/building a script, and for general user-specific queries which would at the worst, leak the users own information to themself. Good luck, -Scott aleczapka _at gmx dot net
If you want to display errors like "Access denied...", when mysql_error() returns "" and mysql_errno() returns 0, use $php_errormsg. This Warning will be stored there. You need to have track_errors set to true in your php.ini. Note. There is a bug in either documentation about error_reporting() or in mysql_error() function cause manual for mysql_error(), says: "Errors coming back from the MySQL database backend no longer issue warnings." Which is not true. miko_il
Following are error codes that may appear when you call MySQL from any host language: http://www.mysql.com/doc/en/Error-returns.html gianluigi_zanettini-megalab.it
A friend of mine proposed a great solution. <?php $old_track = ini_set('track_errors', '1'); ..... if ($this->db_handle!=FALSE && $db_selection_status!=FALSE) { $this->connected=1; ini_set('track_errors', $old_track); } else { $this->connected=-1; $mysql_warning=$php_errormsg; ini_set('track_errors', $old_track); throw new mysql_cns_exception(1, $mysql_warning . " " . mysql_error()); } ?> |
Change Languagemysql_affected_rows mysql_change_user mysql_client_encoding mysql_close mysql_connect mysql_create_db mysql_data_seek mysql_db_name mysql_db_query mysql_drop_db mysql_errno mysql_error mysql_escape_string mysql_fetch_array mysql_fetch_assoc mysql_fetch_field mysql_fetch_lengths mysql_fetch_object mysql_fetch_row mysql_field_flags mysql_field_len mysql_field_name mysql_field_seek mysql_field_table mysql_field_type mysql_free_result mysql_get_client_info mysql_get_host_info mysql_get_proto_info mysql_get_server_info mysql_info mysql_insert_id mysql_list_dbs mysql_list_fields mysql_list_processes mysql_list_tables mysql_num_fields mysql_num_rows mysql_pconnect mysql_ping mysql_query mysql_real_escape_string mysql_result mysql_select_db mysql_set_charset mysql_stat mysql_tablename mysql_thread_id mysql_unbuffered_query |