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

Joe*_*Joe 2 c c++ mysql 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 executing this code
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道C/C++在变量(unlink php)时是非常严格的,但我无法想出这个.有人有什么想法吗?

Fre*_*Foo 11

你正在用指向常量数组row2[2]的指针进行比较.charchar"0"

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