jordanwb Posted October 13, 2011 Share Posted October 13, 2011 I'm writing a program in C which requires MySQL. I wrote a little test program to make sure I know how to use the API. The problem is a keep getting a segmentation fault when I call mysql_num_fields on line 23: #include <stdio.h> #include <mysql/mysql.h> int main (int argc, char **argv) { mysql_library_init(0, NULL, NULL); MYSQL *database = mysql_init (NULL); if (mysql_real_connect (database, "127.0.0.1", "root", "", "my_database", 0, NULL, 0) == NULL) { return 1; } char *query = "SELECT * FROM my_table"; int result = mysql_query (database, query); MYSQL_RES *res = mysql_store_result (database); // do stuff with result unsigned int num_fields = mysql_num_fields(res); MYSQL_ROW row; while (row = mysql_fetch_row(res)) { unsigned long *lengths; lengths = mysql_fetch_lengths(res); for(int i = 0; i < num_fields; i++) { printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL"); } printf("\n"); } mysql_free_result(res); mysql_close (database); mysql_library_end(); return 0; } Quote Link to comment https://forums.phpfreaks.com/topic/249031-segfault-with-mysql_num_fields-using-c-api/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.