You can call the mysql_errno() function to return the error code for the most recently called API function that can succeed or fail.
Syntax
unsigned int
mysql_errno(MYSQL *mysql)
Return values
An error code value for the last mysql_xxx() call is returned for a failure.
For a connection specified by mysql, if mysql_errno() returns 0, it means no error occurred.
Errors
None.
Notes
Client error message numbers are listed in the errmsg.h header file. Server error message numbers are listed in mysqld_error.h.
Some functions such as mysql_fetch_row() do not set mysql_errno() if they succeed. As a rule, all functions that ask the server for information will reset mysql_errno() if they succeed.
Specific error numbers returned by mysql_errno() differ from SQLSTATE values returned by mysql_sqlstate(). For example, the OBClient program displays errors in the following format, where 1146 is the mysql_errno() value and '42S02' is the corresponding mysql_sqlstate() value:
shell> SELECT * FROM no_such_table;
ERROR 1146 (42S02): Table 'test.no_such_table' doesn't exist