List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:July 18 1999 3:20am
Subject:Re: Basic mysql C client question
View as plain text  
At 9:48 PM -0500 7/17/99, Chris Frost wrote:
>I'm trying to write a very simply C program (just over a hundred lines) to
>demonstrate how one can connect to mysql, submit a query, and display the
>results. I'm able to do everything up to displaying the results, where I
>get "(null)" back. I'm a fairly new C programmer (so this is something
>basic I'm sure), and think it has to do with either one of these lines or

One big problem with your program is that you do almost no error
checking.  What happens if mysql_real_connect() fails.  You're
going to crash, because the program continues on as if nothing
is wrong.

>query_out_ptr = mysql_store_result(&mysql); /* store output from
>						   * query */

The program you attached actually has those lines as:

	/* FIXME: Good chance this is where our main problem lays */
	query_out_ptr->data = mysql_store_result(&mysql); /* store output of
						       * query */

which is incorrect.  The ->data should be removed, so the lines
you included in your message are correct.

>printf("%s \n",; /* print our query data */

You shouldn't even be trying to access  Why are you
doing this?  If you're trying to get the rows returned by your query
and print their contents, use mysql_fetch_row().  That seems to be
what you're doing later in your program.

One thing that looks very weird to me is that you issue the query,
and then, if it's successful, invoke mysql_list_tables():

	else // query succeeded, process any data returned by it
		printf("Debug: query seems to have succceeded\n");
		printf("Debug: info: tables in %s database: %s\n\n", db,
			mysql_list_tables(&mysql, search_tables_ptr));

mysql_list_tables() actually runs a query, so this is likely to mess
up the query that you're trying to find out about.

Also, you have this early in the program:

	/* length of a returned field. we have no idea how long it is, but 100
	 * sounds like it may work (hmm, this isn't ugly) */
	/* sample code simply stored this value into an unreferenced pointer */
	unsigned long lengths[100], *lengths_ptr;

Which you use later in your row-fetching loop as:

	while ((row = mysql_fetch_row(query_out_ptr)))
		printf("\tDebug: while loop %d in progress.\n", row);
		printf("\tDebug: %d == row ==
		lengths_ptr = mysql_fetch_lengths(query_out_ptr);
		printf("\tDebug: for loop beginning.\n");
		/* number of columsn is one if only one total, but
		 * since we start at 0 (one less), we only do a less
		 * than and not an equals */
		/* debuging note: if you tell this to loop however many
		 * times as there are columns, it will return all
		 * data */
		for (i = 0; i < (int) num_query_columns; i++)
			printf("[%.*s] ", (int) lengths[i], row[i] ?
					row[i] : "NULL");
		printf("\n"); /* return character for above printf,
			       * this way we can print out multiple
			       * columns*/
		printf("\tDebug: for loop completed. (%d loop(s).)\n",

So you fetch the column lengths into lengths_ptr, but then in the
print loop you use lengths.  lengths is never set to anything.
You should be using lengths_ptr[i] in the printf() statement.

My advice would be:

1) Put in error checking for *every* function that can fail, and
act on errors if they occur.

2) Make your program a lot simpler.  Get something simple to work,
then start adding to it.  This is especially important if, as you
say, you're a new C programmer.  The program's just way too complicated
for what it's trying to do.

3) Spend some time reading the C API chapter in the MySQL manual.
Some of the broken parts of your example program would be cured
if you followed the examples there more closely, I suspect.

Good luck.

Paul DuBois, paul@stripped
Basic mysql C client questionChris Frost18 Jul
  • Re: Basic mysql C client questionPaul DuBois18 Jul
    • Re: Basic mysql C client questionChris Frost18 Jul
      • Re: Basic mysql C client questionPaul DuBois18 Jul
  • Re: Basic mysql C client questionEd Carp18 Jul
    • Re: Basic mysql C client questionSasha Pachev18 Jul