MYSQL *mysql_init(MYSQL *mysql)
Description
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.
Return Values
An initialized MYSQL*
handle.
NULL
if there was insufficient memory to
allocate a new object.
Errors
In case of insufficient memory, NULL
is
returned.
User Comments
Is neccessary to call mysql_init before every calling of mysql_real_connect()? Is next code good?
MYSQL m;
mysql_init(&m);
mysql_real_connect(&m,...);
...
mysql_close(&m);
/* New connection without mysql_init(). */
mysql_real_connect(&m,...);
...
It is necessary to re-mysql_init() a MYSQL struct once you've mysql_close()'d it, as the mysql_close (20.2.3.4) 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(). "
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.
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.
Add your own comment.