mysql_stmt_fetch()返回100?

时间:2015-10-06 11:10:43

标签: mysql c prepared-statement mysql-5.6

我正在尝试在c服务中做一个准备好的语句一切看起来没问题,直到调用mysql_stmt_fetch(),因为它返回100并且我在mysql文档中找不到这个的原因?

示例代码:

int Check_Unique(char *sumin)
{
  //DATABASE INFORMATION
  MYSQL db;
  MYSQL_STMT *stmt;
  MYSQL_BIND param[1],result[1];

  char *l_statement;
  //CONNECTING TO THE DATABASE...
  if(mysql_init (&db)==NULL)
  {
    if(gDEBUG==1){printf("\nFailed to connect to db\n");}
    return(5);
  }
        if(mysql_real_connect(&db,DB_IP,DB_USERNAME,DB_PASSWORD,DB_DATABASE,0,NULL,0) == NULL)
  {
    return(5);
  }

  l_statement = "SELECT used FROM sumplace WHERE sumin = ? LIMIT 1;";
  if(gDEBUG==1){printf("\nSQL: %s\n",l_statement);}

  int result_data = -1;
  unsigned long str_length;
  unsigned long data_length;

  stmt = mysql_stmt_init(&db);
  if(!stmt)
  {
    if(gDEBUG==1){printf("\ninit Failed\n");}
    mysql_close(&db);
    return(-10);
  }
  if(mysql_stmt_prepare(stmt, l_statement, strlen(l_statement)) != 0)
  {
    if(gDEBUG==1){printf("\nstmt_prepare Failed : %s\n", mysql_stmt_error(stmt));}
    mysql_close(&db);
    return(-5);
  }

  memset(param, 0, sizeof(param));
  memset(result, 0, sizeof(result));

  param[0].buffer_type = MYSQL_TYPE_STRING;
  param[0].buffer = (char *)sumin;
  param[0].buffer_length = strlen(sumin);
  param[0].is_null = 0;
  param[0].length = &str_length;

  result[0].buffer_type = MYSQL_TYPE_LONG;
  result[0].buffer = (char *)&result_data;
  result[0].buffer_length = 4;
  result[0].is_null = 0;
  result[0].length = &data_length;

  if(gDEBUG ==1){printf("\n%s\n",(char *)param[0].buffer);}
  if(mysql_stmt_bind_param(stmt, param)) //bind parameters
  {
    if(gDEBUG==1){printf("\nbind_param failed\n");}
    mysql_close(&db);
    return(-10);
  }  
  if(mysql_stmt_bind_result(stmt, result) != 0) //bind results
  {
    if(gDEBUG==1){printf("\nbind_result Failed\n");}
    mysql_close(&db);
    return(-10);
  }
  if(mysql_stmt_execute(stmt) != 0) //execute statement
  {
    if(gDEBUG==1){printf("\nstatement execution Failed\n");}
    mysql_close(&db);
    return(-10);
  }
  if(gDEBUG==1) printf("result_data[%d]\n", result_data);
  int i = mysql_stmt_fetch(stmt);
  if(i == 0) //fetch data from return
  {
    if(gDEBUG==1) printf("Prepared Success result_data[%d]\n", result_data);
    if(result_data == 0)
    {
      if(gDEBUG==1) printf("Sumin Valid!!!\n");
      mysql_stmt_close(stmt);
      mysql_close(&db);
      return(1);
    }
    else if(result_data == 1)
    {
      if(gDEBUG==1) printf("Sumin Invalid!!!\n");
      mysql_stmt_close(stmt);
      mysql_close(&db);
      return(3);
    }
    else
    {
      if(gDEBUG==1) printf("Sumin Invalid!!!\n");
      mysql_stmt_close(stmt);
      mysql_close(&db);
      return(2);
    }
  }
  else
  {
    if(gDEBUG==1){printf("\n%d\n", i);}
    if(gDEBUG==1){printf("\nmysql_stmt_fetch Failed : %s\n", mysql_stmt_error(stmt));}
    if(gDEBUG==1) printf("Prepared Failure/No Results Found\n");
  }

  mysql_stmt_close(stmt);
  mysql_close(&db);
  return(2);
}

示例结果:

SQL: SELECT sumin FROM sumwhere WHERE sumin = ? LIMIT 1;

####

result_data[-1]

100

mysql_stmt_fetch Failed : 
Prepared Failure/No Results Found

1 个答案:

答案 0 :(得分:2)

我有一些同情,因为mysql_stmt_fetch的文档没有告诉你两个可能的返回结果的实际值。但是,请参阅this bug report并告诉您100意味着。

这意味着您的搜索没有返回任何结果(MYSQL_NO_DATA)。