The client library functions may be classified into the following categories:
Connection management routines to establish and terminate connections to the Server
Status and error-reporting routines to get error codes and messages
Query construction and execution routines to construct queries and send them to the server
Result set processing routines to handle results from queries that return data
Information routines that provide information about the client, server, protocol version, and the current connection
Administration routines for controlling server operation
Debugging routines to generate debugging information
Deprecated routines that now are considered obsolete
Client library functions within each of these categories are described in detail in the following sections, listed alphabetically within category. Certain parameters appear recurrently in the function descriptions and have a standard meaning:
conn is a pointer to the MYSQL connection handler for a server connection.
res_set is a pointer to a MYSQL_RES result set structure.
field is a pointer to a MYSQL_FIELD column information structure.
row is a MYSQL_ROW data row from a result set.
For brevity, where these parameters are not mentioned in the descriptions of functions in which they occur, you may assume the meanings just given.
Unless otherwise indicated, you may assume any given function is present in the client library at least as far back as MySQL 3.21.0.
These functions allow you to establish and terminate connections to a server, to set options affecting the way connection establishment occurs, and to re-establish connections that have timed out.
my_bool
mysql_change_user (MYSQL *conn, char *user_name, char *password, char
*db_name);
Changes the user and the default database on the connection specified by conn. The database becomes the default for table references that do not include a database specifier. If db_name is NULL, no default database is selected.
mysql_change_user() returns true if the user is allowed to connect to the server and, if a database was specified, has permission to access the database. Otherwise, the function fails and the current user and database remain unchanged.
void
mysql_close (MYSQL *conn),)
Closes the connection specified by conn. Call this routine when you are done with a server session. If the connection handler was allocated automatically by mysql_init(), mysql_close() de-allocates it.
Do not call mysql_close() if the attempt to open a connection failed.
MYSQL *
mysql_init (MYSQL *conn);
Initializes a connection handler and returns a pointer to it. If conn points to an existing MYSQL structure, mysql_init() initializes that handler and returns its address:
MYSQL conn_struct, *conn; conn = mysql_init (&conn_struct);
If conn is NULL, mysql_init() allocates a new handler, initializes it, and returns its address:
MYSQL *conn; conn = mysql_init (NULL);
If mysql_init() fails, it returns NULL. This may happen if mysql_init() cannot allocate a new handler.
If mysql_init() allocates the handler, mysql_close() de-allocates it automatically when you close the connection.
int
mysql_options (MYSQL *conn, enum mysql_option option, char *arg);
This function allows you to tailor connection behavior more precisely than is possible with mysql_real_connect() alone. Call it after mysql_init() and before mysql_real_connect(). You may call mysql_options() multiple times if you want to set several options.
The option argument specifies which connection option you want to set. Additional information needed to set the option, if any, is specified by the arg argument. (Note that arg is a pointer.) arg is NULL if no additional information is needed.
The following options are available:
MYSQL_INIT_COMMAND
Specifies a query to execute after connecting to the server. arg is a null-terminated string containing the query. The query is executed after reconnecting as well (for example, if you call mysql_ping()). Any result set returned by the query is discarded.
MYSQL_OPT_COMPRESS
Specifies that the connection should use the compressed client/server protocol if the server supports it. arg is NULL.
It is also possible to specify compression when you callmysql_real_connect().
MYSQL_OPT_CONNECT_TIMEOUT
Specifies the connection timeout, in seconds. arg is a pointer to an unsigned int containing the timeout value.
MYSQL_OPT_NAMED_PIPE
Specifies that the connection to the server should use a named pipe. arg is NULL. This option is for Windows 95/98/NT clients only and only for connections to Windows NT servers.
MYSQL_READ_DEFAULT_FILE
Specifies an option file to read for connection parameters. Options are read from the [client] group in the file.
MYSQL_READ_DEFAULT_GROUP
Specifies a group to read from the option file that you name with MYSQL_READ_DEFAULT_FILE. The named group is read in addition to the [client] group. If no option file is named, the client library looks for the standard option files and reads them.
If you call mysql_options() multiple times to set a given option, mysql_real_connect() uses the most recently specified value for that option.
mysql_options() returns zero for success and non-zero if the option value is unknown.
The following options may be used in an option file (in either the [client] group or a group that you specify with the MYSQL_READ_DEFAULT_GROUPoption):
compress database=db_name debug host=host_name init-command=query password=your_password pipe port=port_num return-found-rows socket=socket_name timeout=seconds user=user_name
The mysql_options() calls in the following example set connection options so that mysql_real_connect() connects with a named pipe using the compressed protocol and a timeout of 10 seconds. It reads C:my.cnf.special for information from the [client] and [mygroup] groups. When the connection is established, a SET SQL_BIG_TABLES statement is executed.
MYSQL *conn; unsigned int timeout; if ((conn = mysql_init (NULL)) == NULL) … deal with error … timeout = 10; mysql_options (conn, MYSQL_OPT_CONNECT_TIMEOUT, (char *) &timeout); mysql_options (conn, MYSQL_OPT_COMPRESS, NULL); mysql_options (conn, MYSQL_OPT_NAMED_PIPE, NULL); mysql_options (conn, MYSQL_READ_DEFAULT_FILE, "C:my.cnf.special"); mysql_options (conn, MYSQL_READ_DEFAULT_GROUP, "mygroup"); mysql_options (conn, MYSQL_INIT_COMMAND, "SET SQL_BIG_TABLES=1"); if (mysql_real_connect (conn, …) == NULL) … deal with error …
mysql_options() was introduced in MySQL 3.22.1. MYSQL_INIT_COMMAND, MYSQL_READ_DEFAULT_FILE, and MYSQL_READ_DEFAULT_GROUP were introduced in MySQL 3.22.10.
int
mysql_ping (MYSQL *conn);
Checks whether the connection indicated by conn is still up. If not, mysql_ping() reconnects using the same parameters that were used initially to make the connection. Thus, you should not call mysql_ping() without first successfully having called mysql_real_connect().
mysql_ping() was introduced in MySQL 3.22.1.
MYSQL *
mysql_real_connect (MYSQL *conn, char *host_name,
char *user_name, char
*password, char *db_name,
unsigned int port_num, char
*socket_name,
unsigned int flags);
Connects to a server and returns a pointer to the connection handler. conn should be a pointer to an existing connection handler that was initialized by mysql_init().The address of the handler is the return value for a successful connection. NULL is returned if an error occurs.
If the connection attempt fails, you can pass the conn handler value to mysql_errno() and mysql_error() to obtain error information. However, you should not pass the conn value to any other client library routines that assume the handler reflects a successful connection.
host_name is the server to connect to. Table F.4 shows how the client attempts to connect for various host_name values for UNIX and Windows clients. (If you connect using a UNIX socket or named pipe, the socket_name parameter specifies the socket or pipe name.)
user_name is your MySQL username. If this is NULL, the client library sends a default name. Under UNIX, the default is your login name. Under Windows, the default is your name as specified in the USER environment variable if that variable is set and "ODBC" otherwise.
password is your password. If this is NULL, you will only be able to connect if the user grant table contains an entry matching your username and the host from which you are connecting and that entry has a blank password.
db_name is the database to use. If this is NULL, no initial database is selected.
port_num is the port number to use for TCP/IP connections. If this is 0, the default port number is used.
socket_name is the socket name to use for connections to the local host. If this is NULL, the default socket name is used.
The port number and socket name are used according to the value of host_name. If you are connecting to the local host, mysql_real_connect() attempts a connection using a UNIX domain socket (under UNIX) or a named pipe (under Windows). Otherwise, it connects using TCP/IP.
flags can be 0 to specify no options or one or more of the values shown in Table F.5. The options affect the operation of the server.
Flag Value | Effect on Server Operation |
---|---|
CLIENT_FOUND_ROWS | For UPDATE queries, return the number of rows matched rather than the number of rows changed. |
CLIENT_NO_SCHEMA | Don't allow db_name.tbl_name.col_name syntax. |
CLIENT_COMPRESS | Use compressed communications protocol, if the server supports it. |
CLIENT_ODBC | Treat the client as an ODBC client. |
If you specify CLIENT_NO_SCHEMA, the server allows only references of the forms tbl_name.col_name, tbl_name, or col_name in queries.
The flag values are bit values, so you can combine them using '|' or '+', for example, CLIENT_COMPRESS | CLIENT_ODBC or CLIENT_COMPRESS+CLIENT_ODBC.
mysql_real_connect() was introduced in MySQL 3.21.10. The db_name parameter was added in 3.22.0. The use of mysql_init() to initialize MYSQL arguments began in 3.22.1.
The functions in this section allow you to determine and report the causes of errors.
unsigned int
mysql_errno (MYSQL *conn);
Returns an error code for the most recently invoked client library routine that returned a status. The error code is 0 if no error occurred and non-zero otherwise. The possible error codes are listed in the MySQL header files errmsg.h and mysqld_error.h.
if (mysql_errno (conn) == 0) printf ("Everything is okay "); else printf ("Something is wrong! ");
mysql_errno() was introduced in MySQL 3.21.7.
char *
mysql_error (MYSQL *conn);
Returns a null-terminated string containing an error message for the most recently invoked client library routine that returned a status. The return value is the empty string if no error occurred (this is the zero-length string "" not a NULL pointer). Although normally you call mysql_error() after you already know an error occurred, the return value itself can be used to detect the occurrence of an error:
if (mysql_error (conn)[0] == ' ') /* empty string? */ printf ("Everything is okay "); else printf ("Something is wrong! ");
mysql_error() was introduced in MySQL 3.21.7 (possibly a little later).
The functions in this section allow you to send queries to the server.
mysql_escape_string() helps you construct queries by escaping characters that need special treatment.
unsigned int
mysql_escape_string (char *to_str, char *from_str, unsigned int
from_len);
Encodes a string that may contain special characters so that it can be used in an SQL statement. Table F.6 lists the characters that are considered special and their encodings.
Special Character | Encoding |
---|---|
NUL (ASCII 0) |