mysql_init()
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 they were when building the library.
Although this function prepares a handle for a database connection, no connection is attempted. Here is an example:
...
MYSQL *mysql;
if(mysql_init(mysql) == NULL)
{
printf("Could not initialize MySQL object. \n");
return 1;
}
...