Bug 164801 - C API fails to process simple query
C API fails to process simple query
Product: Fedora
Classification: Fedora
Component: mysql (Show other bugs)
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Tom Lane
David Lawrence
Depends On:
  Show dependency treegraph
Reported: 2005-08-01 11:50 EDT by Mike
Modified: 2013-07-02 23:06 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2005-09-30 14:43:25 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Mike 2005-08-01 11:50:31 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050524 Fedora/1.0.4-4 Firefox/1.0.4

Description of problem:
Using the following C code (test.c):

#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>

int main (int argc, char *argv) {
        MYSQL demo_db;
        MYSQL *connect_status;
        MYSQL_RES *result;
        MYSQL_ROW row;
        MYSQL_FIELD *field;
        char *query = malloc(255);
        connect_status = mysql_real_connect( &demo_db,
                "**************", // username
                "**************", // password
                3306, NULL, 0 );
        if( !connect_status ) {
                fprintf(stderr,"Failed to connect to database: Error: %s\n",
        } else { printf("Connected to database\n");}
        sprintf(query,"SHOW TABLES");
        if( mysql_real_query(&demo_db, query, 255) ){
                printf("find_query error with query: \"%s\"\n",query);
                fprintf(stderr,"REPORTED ERROR : %s",mysql_error(&demo_db));
        } else { printf("Find Query processed.\n"); }
        result = mysql_store_result(&demo_db);
        row = mysql_fetch_row(result);
        printf("Row[0] = %s\n", row[0]);

        return 0;

and compiling with this (gcc-4.0.0-8):

        gcc -lmysqlclient -I/usr/include/mysql -L/usr/lib/mysql src/test.c -o test

I get this:

Connected to database
find_query error with query: "SHOW TABLES"
REPORTED ERROR : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

"SHOW TABLES" as SQL syntax error? methinks something is rotten in the state of denmark.

BTW, using command line mysql, SHOW TABLES works fine.

Similar code worked fine on FC3 - will confirm same code above works fine on FC3 later today. If no follow-up, assume code worked and that bug is FC4-only.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Copy code
2. Compile code
3. Run code
4. Roll eyes

Actual Results:  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Expected Results:  +-------------------+
| Tables_in_test    |
| _fts_test         |
| _fts_test_data    |
| _fts_test_words   |
| _sl_test_stoplist |
4 rows in set (0.00 sec)

Additional info:
Comment 1 Tom Lane 2005-09-30 14:43:25 EDT
I think your code is faulty: you're passing string length 255 to
mysql_real_query, when the string actually passed is only 12 characters counting
the trailing null.  The other 243 bytes contain garbage --- probably \0
characters in this test program, but in general the contents of a freshly
malloc'd block are not guaranteed.  Changing the "255" to "strlen(query)"
makes it work for me.

I don't know why it appeared to work on FC3; maybe MySQL 3.x silently ignored
null characters in the supplied query string.  But I see no bug here.

Note You need to log in before you can comment on or make changes to this bug.