Hướng dẫn echo mysql error

[PHP 4, PHP 5]

mysql_errorReturns the text of the error message from previous MySQL operation

Description

mysql_error[resource $link_identifier = NULL]: string

Parameters

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect[] is assumed. If no such link is found, it will try to create one as if mysql_connect[] had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

Return Values

Returns the error text from the last MySQL function, or '' [empty string] if no error occurred.

Examples

Example #1 mysql_error[] example

The above example will output something similar to:

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

See Also

  • mysql_errno[] - Returns the numerical value of the error message from previous MySQL operation
  • » MySQL error codes

aleczapka _at] gmx dot net

18 years ago

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.

Florian Sidler

12 years ago

Be aware that if you are using multiple MySQL connections you MUST support the link identifier to the mysql_error[] function. Otherwise your error message will be blank.

Just spent a good 30 minutes trying to figure out why i didn't see my SQL errors.

Pendragon Castle

13 years ago

Using a manipulation of josh >'s function, I created the following. It's purpose is to use the DB to store errors. It handles both original query, as well as the error log. Included Larry Ullman's escape_data[] as well since I use it in q[].

Chủ Đề