You can call the mysql_sqlstate() function to return a null-terminated string that contains the SQLSTATE error code for the most recently executed SQL statement.
Syntax
const char *
mysql_sqlstate(MYSQL *mysql)
Return values
A null-terminated string containing the SQLSTATE error code is returned.
Errors
For more information, see mysql_errno(), mysql_error(), and mysql_stmt_sqlstate().
Notes
An error code consists of five characters. "00000" means no error. The values are specified by ANSI SQL and ODBC.
The SQLSTATE values returned by mysql_sqlstate() differ from the error numbers returned by mysql_errno(). For example, the mysql 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
Not all error numbers are mapped to SQLSTATE error codes. The value 'HY000' (general error) is used for unmapped error numbers.
If you call mysql_sqlstate() after mysql_real_connect() fails, mysql_sqlstate() might not return a useful value. For example, this happens if a host is blocked by the server and the connection is closed without any SQLSTATE value being sent to the client.