Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 37.8Mb
PDF (A4) - 37.8Mb
PDF (RPM) - 36.4Mb
HTML Download (TGZ) - 9.8Mb
HTML Download (Zip) - 9.9Mb
HTML Download (RPM) - 8.6Mb
Man Pages (TGZ) - 209.5Kb
Man Pages (Zip) - 318.7Kb
Info (Gzip) - 3.5Mb
Info (Zip) - 3.5Mb
Excerpts from this Manual mysql_connect()

MYSQL *mysql_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd)


This function is deprecated. Use mysql_real_connect() instead.

mysql_connect() attempts to establish a connection to a MySQL database engine running on host. mysql_connect() must complete successfully before you can execute any of the other API functions, with the exception of mysql_get_client_info().

The meanings of the parameters are the same as for the corresponding parameters for mysql_real_connect() with the difference that the connection parameter may be NULL. In this case, the C API allocates memory for the connection structure automatically and frees it when you call mysql_close(). The disadvantage of this approach is that you cannot retrieve an error message if the connection fails. (To get error information from mysql_errno() or mysql_error(), you must provide a valid MYSQL pointer.)

Return Values

Same as for mysql_real_connect().


Same as for mysql_real_connect().

User Comments
  Posted by Ingo Tomahogh on December 9, 2004
If you need to compile old programs still using this function, you might want to add the following macro definition to your programs (possibly via the compiler's command line) so you needn't change all calls to mysql_connect() :

#define mysql_connect(m,h,u,p) mysql_real_connect((m),(h),(u),(p),NULL,0,NULL,0)

You should also make sure the MYSQL pointer is never NULL in these calls, though.
  Posted by David Phillips on February 9, 2005
In response to the mysql_connect() macro, also make sure to call mysql_init() before using it. You didn't need to (or at least it worked) with older versions of the library. You will immediately crash if you don't.
  Posted by Svyatoslav Kovalsky on May 25, 2006
Since mysql_connect(...) can take NULL or uninitialized MYSQL structure as first parameter, it would be better to use following code to make old programs work:

MYSQL * subst_mysql_connect(MYSQL * mysql, const char *host, const char *user, const char *passwd)
MYSQL * mysql_handle;

return mysql_real_connect(mysql_handle,host,user,passwd,NULL,0,NULL,0);

#define mysql_connect(m,h,u,p) subst_mysql_connect((m),(h),(u),(p))

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