I'm working on a simple c program that has to connect to my database, then do a query and then close the connection.
int main()
{
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error %u %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_real_connect(conn, "localhost", "root", "root", NULL, 8889, NULL, 0)) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "create database testdb")) {
printf("Error %u: %s",mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_close(conn);
return 0;
}
This code compiles but when I run it, it will exit after the mysql_query()
statement.
The following error is returned:
Error 2006: MySQL server has gone away
I used google to search for an answer and ended up here:
Your connection's failing - your test should be:
mysql_real_connect returns NULL on failure, or the connection handle on success.
--Dave
/* Simple C program that connects to MySQL Database server*/
http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html
your connection is not being made