Bug #41494 out of sync, trying fetch 2 prepared statements
Submitted: 16 Dec 2008 11:27 Modified: 16 Dec 2008 17:14
Reporter: Oscar Núñez Espinosa Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S2 (Serious)
Version:5.0.67 OS:Linux (Ubuntu 8.10)
Assigned to: CPU Architecture:Any
Tags: C API, error 2014, out of sync, prepared statements

[16 Dec 2008 11:27] Oscar Núñez Espinosa
Description:
Hello, I'am trying run a simple example from prepared statements using the C API and I have arrived to conclusion that is impossible.
Is very simple, I create, prepare, execute and fetch one prepare statement and later i want do the same with another prepared statement maintaining the first opened for fetch it, I don't store results because like say the reference manual I can fetch row by row.

How to repeat:
Teh code is :

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#define def_host_name "localhost" /* host to connect to (default = localhost) */
#define def_user_name "root"      /* user name (default = your login name) */
#define def_password  "guacha"    /* password (default = none) */
#define def_db_name   "sonybcd"   /* database to use (default = none) */

int main(int argc, char *argv[])
{
    MYSQL *conn; /* pointer to connection handler */
    MYSQL_STMT *stmnt, *stmnt2;
    MYSQL_BIND vars_bind;
    int i=0, z=0;
    char area[11];
    char query1[150], query2[150];

    strcpy(query1,"SELECT AREA FROM pepe");
    strcpy(query2,"SELECT AREA FROM jose");

    memset(&vars_bind,0,sizeof(vars_bind));
    memset(&stmnt,0,sizeof(stmnt));
    memset(&stmnt2,0,sizeof(stmnt2));
    memset(&conn,0,sizeof(conn));

    if (!(conn = mysql_init (NULL)))
    {
        fprintf (stderr, "mysql_init() failed (probably out of memory)\n");
        exit (1);
    }
    printf("Creada conexión.\n");
    if (!mysql_real_connect (conn,def_host_name,def_user_name,def_password,def_db_name,0,NULL,0))
    {
        fprintf(stderr, "mysql_real_connect() failed:\nError %u (%s)\n",mysql_errno(conn), mysql_error(conn));
        exit (1);
    }

    printf("Conectado a %s.\n",mysql_get_host_info(conn));
    printf("MySQL client version: %s\n", mysql_get_client_info());

    if (!(stmnt2 = mysql_stmt_init(conn)))
    {
        fprintf (stderr, "mysql_stmt_init() failed (probably out of memory)\n");
        exit (1);
    }
    if (mysql_stmt_prepare(stmnt2, query1, strlen(query1)))
    {
        fprintf(stderr, "mysql_stmt_prepare() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt2), mysql_stmt_error(stmnt2));
        exit (1);
    }
    if (!(stmnt = mysql_stmt_init(conn)))
    {
        fprintf (stderr, "mysql_stmt_init() failed (probably out of memory)\n");
        exit (1);
    }
    if (mysql_stmt_prepare(stmnt, query2, strlen(query2)))
    {
        fprintf(stderr, "mysql_stmt_prepare() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
        exit (1);
    }

    if (mysql_stmt_execute(stmnt2))
    {
        fprintf(stderr, "mysql_stmt_execute() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt2), mysql_stmt_error(stmnt2));
        exit (1);
    }
    //In this point I obtain the error 2014 "Commands out of sync; you can't run this command now"
    if (mysql_stmt_execute(stmnt))
    {
        fprintf(stderr, "mysql_stmt_execute() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
        exit (1);
    }

    if (mysql_stmt_fetch(stmnt2))
    {
        fprintf(stderr, "mysql_stmt_fetch:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
        exit (1);
    }
    while (!mysql_stmt_fetch(stmnt) && i < 10)
    {
        strcpy(area,"");
        i++;
        while (!mysql_stmt_fetch(stmnt2) && z < 3)
        {
            z++;
        }
    }
    if (mysql_stmt_close(stmnt))
    {
        fprintf(stderr, "mysql_stmt_close() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
        exit (1);
    }
    if (mysql_stmt_close(stmnt2))
    {
        fprintf(stderr, "mysql_stmt_close() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
        exit (1);
    }

    mysql_close (conn);
    printf("Cerrado..\n");

    return EXIT_SUCCESS;
}

the tables are very simple, the two are identical like follows:

CREATE TABLE `jose` (
  `ALMA` tinyint(4) NOT NULL,
  `AREA` varchar(10) NOT NULL,
  `NUMERO` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Suggested fix:
I'm doing anything not correctly?

Allow more than one statement at time, I think that is needed for any simple program.
[16 Dec 2008 14:20] MySQL Verification Team
We're sorry, but the bug system is not the appropriate forum for asking help on using MySQL products. Your problem is not the result of a bug.

Support on using our products is available both free in our forums at http://forums.mysql.com/ and for a reasonable fee direct from our skilled support engineers at http://www.mysql.com/support/

Thank you for your interest in MySQL.
[16 Dec 2008 17:14] Oscar Núñez Espinosa
Sorry, but I have troguht for more than a week solve this problem, i have read these forums and I wasn't able of run two prepared statements simultaneously. 

Following the reference manual say that isn't limited the number of prepared statements, but following the examples isn't possible.

If not is a bug, please, answer only if its possible do it.
Thanks in advance.