MYSQL总是返回FAIL

时间:2011-08-06 00:01:53

标签: c++ mysql

C ++中的这段代码查询MYSQL在线数据库。它似乎总是返回0.即使我知道答案应该是1 PASS。运行时我没有收到错误。

    if ( bShowTCP )
    {
      printf("\n -------------------- // -------------------- ");
      printf("\n TCP Header:");

      int *addressValue = new int();
      char *address = LIP; 
      inet_pton(AF_INET, address, addressValue);
      if (ip_header->source_ip == *addressValue)
      {
         printf("\n   Source      IP: %s", "0.0.0.0");
         printf("\n   Destination IP: %s", ipDest);
      }
      else
      {
         printf("\n   Source      IP: %s", ipSrc);
         printf("\n   Destination IP: %s", ipDest);
         (mysql_real_connect
         (conn,"<hostname>","<db>","<user>","<pass>",0,NULL,0) !=0);
         (mysql_query(conn,"SELECT COUNT(*) FROM tblURLIP WHERE IP = 'ipSrc' And            IPStatus = '1' And IPMax = '0'"));
         my_ulonglong i = 0;
         res_set = mysql_store_result(conn);
         my_ulonglong numrows = mysql_num_rows(res_set);
         LEGIT = mysql_fetch_row(res_set);
         if (atoi(LEGIT[i]) == 1)
         {
           printf("\n PASS: %s\n",LEGIT[i]);
           mysql_free_result(res_set);
         }
         else
         {
           printf("\n FAIL! %s\n",LEGIT[i]);
           mysql_free_result(res_set);
         }             
      }
    }

1 个答案:

答案 0 :(得分:1)

没有变量传递给您的MySQL查询 - WHERE IP = 'ipSrc'实际上是在搜索IP的值为"ipSrc"的行,而不是它等于值ipSrc的行名为ipSrc的字符串变量。您需要使用类似sprintf()的函数将ipSrc的值嵌入到字符串中,或​​者使用预准备语句并将{{1}}绑定到占位符变量。