title: C MySQL Connector - Error 2014 - Commands out of sync when using CALL
body: <p>We are working on an assignment. 
  We are currently trying to call a remote procedure in a MySQL database, but we're getting a 2014 Error and we can't figure out why. We should note that the call is successful and we get the results we expect to get, the problem is in the call to <code>mysql_stmt_close(stmt)</code>.
  We are freeing every MYSQL structure that was generated by the calls to the connector, using <code>mysql_free_result</code>, but the error persists. The error is coming from the <code>mysql_stmt_close(stmt)</code> call, we're getting a 2014 error, as stated in the title. We are freeing every MYSQL structure possible, as far as we can see.</p>
  
  <p>The code is mostly taken from examples over the Internet:</p>
  
  <pre><code>int basedatos_crearUsuario(char * pNombre, MYSQL * pConeccion){
  char * query = "CALL crearUsuario (?)";
  MYSQL_STMT *stmt;
  MYSQL_BIND ps_params[1];  /* input parameter buffers */
  MYSQL_BIND bind_results[1];
  
  stmt = mysql_stmt_init(pConeccion);
  if (!stmt)
  {
      printf("Could not initialize statement\n");
      return -1;
  }
  if (mysql_stmt_prepare(stmt, query, strlen(query))){
      fprintf(stderr, "Error: %s (errno: %d)\n",mysql_error(pConeccion), mysql_errno(pConeccion));
      return -1;
  }
  
  long largo = (long) strlen(pNombre);
  memset(ps_params, 0, sizeof (ps_params));
  ps_params[0].buffer_type = MYSQL_TYPE_STRING;
  ps_params[0].buffer = pNombre;
  ps_params[0].buffer_length = largo;
  ps_params[0].length = &amp;largo;
  ps_params[0].is_null = 0;
  
  if(mysql_stmt_bind_param(stmt, ps_params)){
      fprintf(stderr, "Error: %s (errno: %d)\n",mysql_error(pConeccion), mysql_errno(pConeccion));
      return -1;
  }
  if (mysql_stmt_execute(stmt)){
      fprintf(stderr, "Error: %s (errno: %d)\n",mysql_error(pConeccion), mysql_errno(pConeccion));
      return -1;
  }
  MYSQL_RES* temp;
  if (temp = mysql_stmt_store_result(stmt)) { //
      fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
      return -1;
  }
  MYSQL_RES* aRes = mysql_stmt_result_metadata(stmt);//
  
  MYSQL_FIELD* aField = &amp;(aRes-&gt;fields[0]); 
  int totalrows = mysql_stmt_num_rows(stmt);
  
  int resultadoQuery;
  long largo2 = 0;
  memset (bind_results, 0, sizeof (bind_results));
  bind_results[0].buffer_type= aField-&gt;type;
  bind_results[0].is_null= 0;
  bind_results[0].buffer= (char *) &amp;resultadoQuery;
  bind_results[0].buffer_length= 255;
  bind_results[0].length= &amp;largo2;
  
  mysql_stmt_bind_result(stmt, bind_results);  
  while(!mysql_stmt_fetch(stmt)){
      printf("hay result");
  }
  
  mysql_free_result(aRes);
  mysql_free_result(temp);
  if (mysql_stmt_close(stmt)) //HERE: the stmt_close call returns an error.
  {
    fprintf(stderr, " usuario failed while closing the statement\n");
    fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
    exit(0);
  }
  
  return resultadoQuery;
  </code></pre>
  
  <p>We would appreciate any insight as to why this would happen. The CLIENT_MULTI_RESULTS flag is enabled for the connection, too. We've looked several other similar questions, but we appear to have the flag enabled and the calls in the right order.</p>