MYSQL总是返回失败

MYSQL Returns FAIL Always

本文关键字:失败 返回 MYSQL      更新时间:2023-10-16

这段用c++编写的代码查询MYSQL在线数据库。它似乎总是返回0。即使我知道答案应该是1及格。当它运行时,我没有得到错误。

    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: %sn",LEGIT[i]);
           mysql_free_result(res_set);
         }
         else
         {
           printf("n FAIL! %sn",LEGIT[i]);
           mysql_free_result(res_set);
         }             
      }
    }

没有变量被传递到你的MySQL查询- WHERE IP = 'ipSrc'是字面上搜索行,其中IP有值"ipSrc",而不是那些它等于字符串变量称为ipSrc的值。您需要使用sprintf()之类的函数将ipSrc的值嵌入到字符串中,或者使用准备好的语句并将ipSrc绑定到占位符变量。