#include <stdio.h>
#include <mysql.h>
#include <string.h>
int main(int argc, char **argv)
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
int num_fields;
int i;
char user_query[100];
char word[10];
printf("Enter word: ");
scanf("%s", word);
sprintf(user_query, "select * from english where word = %s", word);
printf("%s\n", user_query);
conn = mysql_init(NULL);
mysql_real_connect(conn, "localhost", "root", "123", "test", 0, NULL, 0);
mysql_query(conn, user_query);
result = mysql_store_result(conn);
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
mysql_free_result(result);
mysql_close(conn);
return 0;
}
Hey there daniweb community. Could anyone tell me why this code seg faults each time i run it. it compiles without errors, runs okay right up until where it prints out the mysql statement to confirmation, but seg faults after that, debug says it receives SIGSEGV signal at the num_fields = mysql_num_fields(result) line.
Thanks for any assistance with this. (Compiled on Ubuntu 12.04, Codeblocks GCC 4.6.3).