Current Location: Home> Function Categories> mysql_errno

mysql_errno

Returns the numeric encoding of the error message in the previous MySQL operation.
Name:mysql_errno
Category:Uncategorized
Programming Language:php
One-line Description:Get the error code for the last MySQL operation

Function name: mysql_errno()

Applicable version: PHP 4, PHP 5, PHP 7

Usage: The mysql_errno() function is used to get the error code of the last MySQL operation.

Syntax: int mysql_errno ( [resource $link_identifier = NULL] )

parameter:

  • link_identifier (optional): MySQL connection identifier. If not specified, the most recently opened connection is used.

Return value: Returns an integer value representing the error code of the most recent MySQL operation, and if no error occurs, return 0.

Example:

  1. Use the default connection identifier to get the error code for the last MySQL operation:
 $conn = mysql_connect("localhost", "username", "password"); mysql_select_db("database", $conn); $query = "SELECT * FROM table"; $result = mysql_query($query); if(mysql_errno() != 0){ echo "MySQL 错误码:" . mysql_errno() . "<br>"; echo "MySQL 错误信息:" . mysql_error() . "<br>"; } else { // 执行成功的操作}
  1. Use the specified connection identifier to get the error code for the last MySQL operation:
 $conn1 = mysql_connect("localhost", "username1", "password1"); $conn2 = mysql_connect("localhost", "username2", "password2"); mysql_select_db("database1", $conn1); mysql_select_db("database2", $conn2); $query1 = "SELECT * FROM table1"; $query2 = "SELECT * FROM table2"; $result1 = mysql_query($query1, $conn1); $result2 = mysql_query($query2, $conn2); if(mysql_errno($conn1) != 0){ echo "MySQL 错误码:" . mysql_errno($conn1) . "<br>"; echo "MySQL 错误信息:" . mysql_error($conn1) . "<br>"; } else { // 执行成功的操作} if(mysql_errno($conn2) != 0){ echo "MySQL 错误码:" . mysql_errno($conn2) . "<br>"; echo "MySQL 错误信息:" . mysql_error($conn2) . "<br>"; } else { // 执行成功的操作}

Notes:

  • Before using the mysql_errno() function, you must execute a MySQL query first, otherwise the error code will not be obtained.
  • After PHP 5.5.0, it is recommended to use the mysqli or PDO extension instead of the mysql function.
Similar Functions
Popular Articles