mysql_error

(PHP 3, PHP 4, PHP 5)

mysql_error --  返回上一个 MySQL 操作产生的文本错误信息

说明

string mysql_error ( [resource link_identifier] )

返回上一个 MySQL 函数的错误文本,如果没有出错则返回 ''(空字符串)。如果没有指定连接资源号,则使用上一个成功打开的连接从 MySQL 服务器提取错误信息。

从 MySQL 数据库后端来的错误不再发出警告,要用 mysql_error() 来提取错误文本。注意本函数仅返回最近一次 MySQL 函数的执行(不包括 mysql_error()mysql_errno())的错误文本,因此如果要使用此函数,确保在调用另一个 MySQL 函数之前检查它的值。

例子 1. mysql_error 例子

<?php
    mysql_connect
("localhost", "mysql_user", "mysql_password");

    
mysql_select_db("nonexistentdb");
    echo
mysql_errno() . ": " . mysql_error(). "\n";

    
mysql_select_db("kossu");
    
mysql_query("SELECT * FROM nonexistenttable");
    echo
mysql_errno() . ": " . mysql_error() . "\n";
?>

以上例子将产生如下输出:

1049: Unknown database 'nonexistentdb'
1146: Table 'kossu.nonexistenttable' doesn't exist

参见 mysql_errno()


add a note add a note User Contributed Notes
l dot poot at twing dot nl
12-Jul-2006 08:38
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><br>' . mysql_error() . '<br><br>';
    
$message .= '<b>Whole query:</b><br>' . $query . '<br><br>';
     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 );
  }

?>
05-Jan-2005 04: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
aleczapka _at) gmx dot net
17-Jul-2004 01:05
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.
olaf at amen-online dot de
23-Jun-2004 02:08
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?
miko_il AT yahoo DOT com
12-Dec-2003 03:25
Following are error codes that may appear when you call MySQL from any host language:

http://www.mysql.com/doc/en/Error-returns.html
josh ><>
14-Oct-2003 07:26
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<br>When executing:<br>\n$query\n<br>";
       return $result;
   }
scott at rocketpack dot net
25-Jul-2003 05:44
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
10-May-2001 10: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 = "" .