views:

28

answers:

2

Heey,

Can somebody help me out?

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 awnser and ended up here:

http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

This couldn't help me find the solution.

Thanks alot.

A: 

Your connection's failing - your test should be:

if (mysql_real_connect(...) == NULL) {
  printf("...");
  exit(1);
}

mysql_real_connect returns NULL on failure, or the connection handle on success.

--Dave

David Knell
A: 

Return Values A MYSQL* connection handle if the connection was successful, NULL if the connection was >unsuccessful. For a successful connection, the return value is the same as the value of the >first parameter.

http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html

your connection is not being made