Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual Excerpts from this Manual mysql_init()

MYSQL *mysql_init(MYSQL *mysql)


Allocates or initializes a MYSQL object suitable for mysql_real_connect(). If mysql is a NULL pointer, the function allocates, initializes, and returns a new object. Otherwise, the object is initialized and the address of the object is returned. If mysql_init() allocates a new object, it is freed when mysql_close() is called to close the connection.

In a nonmulti-threaded environment, mysql_init() invokes mysql_library_init() automatically as necessary. However, mysql_library_init() is not thread-safe in a multi-threaded environment, and thus neither is mysql_init(). Before calling mysql_init(), either call mysql_library_init() prior to spawning any threads, or use a mutex to protect the mysql_library_init() call. This should be done prior to any other client library call.

Return Values

An initialized MYSQL* handle. NULL if there was insufficient memory to allocate a new object.


In case of insufficient memory, NULL is returned.

Download this Manual
User Comments
  Posted by Miloslav Ponkrac on February 22, 2004
Is neccessary to call mysql_init before every calling of mysql_real_connect()? Is next code good?

/* New connection without mysql_init(). */

  Posted by Arlen Cuss on April 12, 2004
It is necessary to re-mysql_init() a MYSQL struct once you've mysql_close()'d it, as the mysql_close ( documentation explains:

"... mysql_close() also deallocates the connection handle pointed to by mysql if the handle was allocated automatically by mysql_init() or mysql_connect(). "
  Posted by Robert Basler on June 9, 2004
This API has one danger. If the structure alignment of your compiler doesn't match the alignment of the compiler that built the library you are linking against, it can overwrite other variables on the stack. So it would be better to let it allocate the structure rather than using the ability to allocate it yourself.

I ran across this when I set my compiler to pack structures and the library was linked with structures aligned to dwords.
  Posted by Leonid Boitsov on September 3, 2004
I have also found that it is dangerous to copy MYSQL structure. With mysql 3.x it was ok, but quite bad with 4.x. Everything works ok, but API hangs up on function mysql_close.

Sign Up Login You must be logged in to post a comment.