Forum Moderators: open
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
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().