Jump to content

segfault with mysql_num_fields using C api


jordanwb

Recommended Posts

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.