Documentation Home
MySQL 8.4 C API Developer Guide
Download this Manual
PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.4Mb


5.4.81 mysql_sqlstate()

const char *mysql_sqlstate(MYSQL *mysql)

Description

Returns a null-terminated string containing the SQLSTATE error code for the most recently executed SQL statement. The error code consists of five characters.'00000' meansno error. The values are specified by ANSI SQL and ODBC. For a list of possible values, seeError Messages and Common Problems.

SQLSTATE values returned bymysql_sqlstate() differ from MySQL-specific error numbers returned bymysql_errno(). For example, themysql client program displays errors using the following format, where1146 is themysql_errno() value and'42S02' is the correspondingmysql_sqlstate() value:

$> SELECT * FROM no_such_table;ERROR 1146 (42S02): Table 'test.no_such_table' doesn't exist

Not all MySQL error numbers are mapped to SQLSTATE error codes. The value'HY000' (general error) is used for unmapped error numbers.

If you callmysql_sqlstate() aftermysql_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.

Return Values

A null-terminated character string containing the SQLSTATE error code.