Forum Moderators: open

Message Too Old, No Replies

Can't seem to get MySQL C API to connect to MySQL

Any C API programmers here?

         

trillianjedi

11:06 am on Jul 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is my first attempt to build a binary in C using the MySQL API (I did a lot of C coding once, but more years ago than I care to remember).

It's initiating OK, but always failing to connect to the DB (password, username etc are absolutely correct).

Any ideas?


/* hellodb.c */

#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>

int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

char query[80];

if(mysql_init(&mysql) == NULL)
{
printf("\nFailed to initate MySQL connection\n");
exit(1);
}

if(mysql_real_connect(&mysql,"localhost","username","password","database",3306,NULL,0) == NULL);
{
printf("\nFailed to connect to MySQL Database\n");
exit(1);
}

printf("MySQL Server Version is %s\n",mysql_get_server_info(&mysql));

mysql_close(&mysql);
return 0;
}

I've also tried setting the PORT parameter to "0".

I'm compiling with:-


gcc hellodb.c -o hellodb -I/usr/local/include -L/usr/local/lib/mysql -lmysqlclient

Thanks for any assistance...

TJ

lammert

2:14 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if(mysql_real_connect(&mysql,"localhost","username","password","database",3306,NULL,0) == NULL);
{
printf("\nFailed to connect to MySQL Database\n");
exit(1);
}

A typical C problem, there is no statement to execute after the if statement because of the semicolon. The printf() and exit() are always executed, regardless of the return value of the call to mysql_real_connect().