Name

mysql_init —

Synopsis

MYSQL *mysql_init(MYSQL *mysql)

Initializes a MYSQL structure for creating a connection to a MySQL database server. A call to this function, followed by one to mysql_real_connect, is currently the approved method of initializing a server connection. You can pass mysql_init either a pointer to a MYSQL structure that you declared or a null pointer. If you pass your own pointer, you are responsible for freeing its data when the time comes (which must be after the connection is closed).

If you pass a null pointer, mysql_init creates a MYSQL structure, initializes it, and returns a pointer to the structure. Structures created by this function are freed automatically when mysql_close is called. A null value is returned if there is not enough memory available to initialize the structure.

Warning

As of the current release of MySQL, MySQL clients will crash on certain platforms (such as SCO Unix) when you pass in a pointer to a MYSQL structure that you allocated yourself. If this is happening to you, just pass in NULL and use the pointer created by the MySQL library. As a bonus, you don’t have to worry about freeing the structure if you do this.

Example

MYSQL mysql;
 
if (!mysql_init(&mysql)) {
                printf("Error initializing MySQL client\n");
                exit(1);
}
/* Now you can call mysql_real_connect(  ) to connect to a server... */
/* Alternative method: */
MYSQL *mysql;

mysql = mysql_init(NULL);
if (!mysql) {
     printf("Error initializing MySQL client\n");
     exit(1);
}

Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.