Name

mysql_init( )

Synopsis

MYSQL *mysql_init(MYSQL *mysql)

This function optionally allocates, and then initializes, a MYSQL object suitable for connecting to a database server and subsequently performing many of the other operations described in this chapter. If the function’s parameter is NULL, the library allocates a new object from the heap; otherwise, the user’s pointed-to local MYSQL object is initialized.

The return value is a pointer to the object however obtained, and a NULL indicates a failure of allocation or initialization. Calling mysql_close( ) with this pointer not only releases the connection-related resources, but also frees the object itself if the library had allocated it in the first place.

It’s generally safer to allow the library to allocate this object rather than to do so yourself: it avoids hard-to-debug complications that can arise if certain compiler options are not in effect while building the application as were when building the library.

Though this function prepares a handle for a database connection, no connection is attempted.

...
MYSQL *mysql;
if(mysql_init(mysql) =  = NULL)
      {
        printf("Could not initialize MySQL object. \n");
        exit(EXIT FAILURE);
      }
...

Get MySQL in a Nutshell 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.