Bug #6059 mysql_stmt_field_count returns positive numbers when no resultset is available
Submitted: 13 Oct 2004 6:57 Modified: 21 Oct 2004 16:19
Reporter: Georg Richter Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S1 (Critical)
Version:>= 4.1 OS:Linux (Linux)
Assigned to: Konstantin Osipov CPU Architecture:Any

[13 Oct 2004 6:57] Georg Richter
Description:
Usually mysql_stmt_field_count will be used to determine if statement produced a resultset which 
can be retrieved by mysql_stmt_fetch. 
 
From the manual:  
Returns the number of columns for the most recent statement for the statement handler. This value 
will be zero for statements such as INSERT or DELETE that do not produce result sets.  
 
However, this is not true, when using SELECT INTO. 
 
Output: 
Fieldcount: 1 
Error in mysql_stmt_fetch: Commands out of sync; you can't run this command now 

How to repeat:
#include <stdio.h> 
#include <mysql.h> 
 
int main () { 
  MYSQL       *mysql = mysql_init(NULL); 
  MYSQL_STMT  *stmt; 
  MYSQL_RES   *res; 
  MYSQL_ROW   row; 
  char        query[500]; 
 
  if (!mysql_real_connect(mysql, "localhost", "root", "", "test", 0, NULL, 0)) { 
      printf("Can't connect! Error: %s\n", mysql_error(mysql)); 
      return 1; 
  } 
 
  stmt = mysql_stmt_init(mysql); 
  strcpy(query, "SELECT 'foo' INTO OUTFILE 'x.3'"); 
 
  if (mysql_stmt_prepare(stmt, query, strlen(query))) { 
    printf("Error in mysql_stmt_prepare: %s\n", mysql_error(mysql)); 
    return 1; 
  } 
 
  if (mysql_stmt_execute(stmt)) { 
    printf("Error in mysql_stmt_execute: %s\n", mysql_stmt_error(stmt)); 
    return 1; 
  } 
 
  printf("Fieldcount: %d\n", mysql_stmt_field_count(stmt)); 
   
  if (mysql_stmt_fetch(stmt)) { 
    printf("Error in mysql_stmt_fetch: %s\n", mysql_stmt_error(stmt)); 
    return 1; 
  } 
 
  mysql_stmt_close(stmt); 
  mysql_close(mysql); 
}
[15 Oct 2004 22:51] Konstantin Osipov
Subject: bk commit - 4.1 tree (konstantin:1.2109) BUG#6059

ChangeSet
  1.2109 04/10/16 02:49:56 konstantin@mysql.com +3 -0
  A fix and test case for bug#6059 "mysql_stmt_field_count returns
  positive numbers when no resultset is available": when sending
  result set metadata we need to use virtual select_result::send_fields,
  and not address protocol directly, because select_result descendents may
  intercept result set (it's the case for example for SELECT INTO OUTFILE).
[21 Oct 2004 16:19] Konstantin Osipov
Fixed in 4.1.7