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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.