C++ linux c/c++;-奇怪的if/else问题

C++ linux c/c++;-奇怪的if/else问题,c++,mysql,c,linux,C++,Mysql,C,Linux,我正在查询一个mysql表,然后该表循环遍历结果 其中一个字段的值为“0”,因此当我尝试以下操作时,它不起作用 while ((row2 = mysql_fetch_row(resultset2)) != NULL) { if (row2[2] != "0") { // the field has a value of 0, but it's still executing the code here! } else { // should be

我正在查询一个mysql表,然后该表循环遍历结果

其中一个字段的值为“0”,因此当我尝试以下操作时,它不起作用

while ((row2 = mysql_fetch_row(resultset2)) != NULL) {
    if (row2[2] != "0") {
        // the field has a value of 0, but it's still executing the code here!
    } else {
        // should be executing this code
    }
}

我知道C/C++在变量(取消php链接)方面非常严格,但我无法理解这一点。有人知道为什么吗?

您正在比较
row2[2]
,一个指向
char
的指针和一个指向常量
char
数组
“0”


使用strcmp(第2行[2],“0”)!=0(C解决方案),
std::string(第2行[2])!=“0”
(C++解决方案)或
atoi(第2行[2])!=0
如果您知道
第2行[2]
始终是整数的字符串表示形式(并且不能是SQL
NULL
值)。

您不能像这样比较字符串文字:

if (row2[2] != "0") //wrong
您可以这样做:

if (strcmp(row2[2], "0")) //correct 
或者这个:

if (std::string(row2[2]) != "0") //correct

对于这种特殊情况,当只有一个字符时,也可以执行以下操作:

if (row2[2][0] != '0') //correct  - not the single quote around 0!

看起来您应该使用字符串比较而不是指针比较我假设第2行保留了某种类型的字符串。您能在条件行之前打印第2[2]行的值吗?C/C++不是一种语言。是C还是C++?完美,这真的帮助了我!