Van wrote:
> 
> In Main, I have:
> MYSQL *conn;    /* Pointer to connection handle */
> 
> Then, I do:
>         if ((conn = do_connect (BTIME_HOST, USER, BTIME_PW, BTIME_DB, 0, NULL, 0)) !=
> NULL)
>         {
>                 /* Validate the structure of the btime database */
>                 check_btime_structure(conn, BTIME_DB);
>                 mysql_close (conn);
>         }
> 
> In, check_btime_structure:
> MYSQL_RES *res_set;
> MYSQL_ROW row;
> char sql[255+1] = "SHOW TABLES FROM ";
> int i =0;
> 
>         strncat(sql, BTIME_DB, sizeof(sql));
>         if ((res_set = do_query(conn, sql)) != NULL)
>         {
>                 while ((row = mysql_fetch_row(res_set)) != NULL)
>                 {
>                         for (i = 0; i < mysql_num_fields (res_set); i++)
>                         {
>                                 check_btime_table (conn, row[i]);
>                         }
>                 }
>         }
> 
>   return EXIT_SUCCESS;
> }
> 
> In check_btime_table:
> int
> check_btime_table(MYSQL *conn, char *BTIME_TABLE)
> /* Validate individual btime table */
> {
> MYSQL_RES *res_set;
> char sql[255+1] = "DESCRIBE ";
> 
>         strncat(sql, BTIME_TABLE, sizeof(sql));
> 
> /*      Cycle through the table description rows and make sure they're in synch */
> 
>         if ((res_set = do_query (conn, sql)) != NULL)
>         {
>                 fprintf(stderr, "sql:  %s%s%s\n", boldon, sql, boldoff);
>         }
>         return EXIT_SUCCESS;
> }
> 
Greetings, again:

No need to reply to this, since I figured out the problem.  It was actually in
the do_query implementation:

Previously the do_query function was:

MYSQL_RES *do_query(MYSQL *conn, char *sql)
{
MYSQL_RES res_set;

        if (mysql_query (conn, sql) != 0)
        {
                // do stuff...
        }
}

After putting in the:
        fprintf(stderr, "do_connect Pointer address is %p\n", res_set);
in the fixed version I realized I was getting the same memory address for the
res_set pointer on each invocation.  This was making the above res_set{s} step
on each other.  It was returning the same address for each call, which confused
the query processing functions.

To ensure each invocation of the do_query function gets a new address, use the
following:
MYSQL_RES *do_query(MYSQL *conn, char *sql, MYSQL_RES *res_set)
        /*      Run a query.  Return the result set. */
{
unsigned int errno = 0;
char *errmsg = NULL;

        if (mysql_query (conn, sql) != 0)               /* btime database doesn't 
exist */
        {
                errno = mysql_errno(conn);
                errmsg = mysql_error(conn);
                
                fprintf(stderr, "There is a problem with the BTime database...\n\n");
                fprintf(stderr, "The actual error was:\n");
                fprintf(stderr, "mysql_query error:  #%u.\n%s\n\n", errno, errmsg);
                fprintf(stderr, "The query was:  %s\n", sql);
                exit(EXIT_FAILURE);
        } else {
                res_set = mysql_store_result (conn);            /* generate result set 
*/
                if (res_set == NULL)
                {
                        errno = mysql_errno(conn);
                        errmsg = mysql_error(conn);
                        
                        fprintf(stderr, "mysql_store_result() failed...\n\n");
                        fprintf(stderr, "The actual error was:\n");
                        fprintf(stderr, "mysql_real_connect error:  #%u.\n%s\n\n", 
errno, errmsg);
                        fprintf(stderr, "The query was:  %s\n", sql);
                        exit(EXIT_FAILURE);
                }
        }

//      fprintf(stderr, "do_connect Pointer address is %p\n", res_set);
//      mysql_free_result(res_set);
        return res_set;
}


Thus, the same connection can be used and note the mysql_store_result (conn)
implementation, rather than mysql_use_result(), which can require more coding
for small result sets.

So throw a MYSQL_RES *res_set in the function declaration that will call the
do_query function, but, not in the do_query function, and pass that pointer to
do_query, which will return the newly allocated pointer in the return.

Ensure you do the mysql_free_result(res_set) in the function that calls the
do_query(), not within do_query().  That's why it's commented out in
NON-C-COMMENTING-NOTATION.  I comment with // on debug functions so don't flame
me.

Thought this might save someone else out there 12 hours worth of strange memory
addressing chasing.

Regards,
Van
-- 
=========================================================================
Linux rocks!!!   http://www.dedserius.com
=========================================================================

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to