MYSQL Returns FAIL Always
This piece of code in C++ queries a MYSQL online database. It seems that it always returns 0. Even when I know the answer should be 1 PASS. I'm not getting an errors when it runs.
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);
}
}
}
There are no variables being passed to your MySQL query -- the WHERE IP = 'ipSrc'
is literally searching for rows where IP
has the value "ipSrc"
, not ones where it's equal to the value of the string variable called ipSrc
. You will need to either embed the value of ipSrc
into a string using a function like sprintf()
, or use a prepared statement and bind ipSrc
to a placeholder variable.
精彩评论