php检查数据库中的行是否存在总是返回true
我想根据“id”和“devicetoken”两列检查db表中是否存在条目 这个函数的结果总是为true,但在数据库中没有这样的条目。这怎么可能php检查数据库中的行是否存在总是返回true,php,mysql,Php,Mysql,我想根据“id”和“devicetoken”两列检查db表中是否存在条目 这个函数的结果总是为true,但在数据库中没有这样的条目。这怎么可能 $selectSQL = "SELECT id from devices where devicetoken = ? and id = ?"; echo "token:".$utoken."\n"; echo "id:".$uid."\n"; $resul
$selectSQL = "SELECT id from devices where devicetoken = ? and id = ?";
echo "token:".$utoken."\n";
echo "id:".$uid."\n";
$resultId = -33;
if ($stmt->prepare($selectSQL)) {
$stmt->bind_param('ii', $utoken, $uid);
$stmt->execute();
$stmt->bind_result($resultId);
$stmt->fetch();
$stmt->close();
echo $resultId;
if ($resultId == $uid) {
echo "AA";
return true;
} else {
echo "BB";
return false;
}
}
使用此参数运行页面时:
令牌:asdf
身份证号码:2
我得到打印的输出2AA
,因为$resultId==$uid
。但这怎么可能呢?select语句清楚地过滤了两个where条件,没有token=asdf和id=2这样的条目。
id=2的实际条目的令牌值为'b'
这是我的桌子的屏幕截图:
如果我在Mysql工作台上运行代码
SELECT id from devices where devicetoken = 'b2' and id = 2
我没有得到任何错误和0个结果,这是正确的。但从PHP页面中,它得到了一个错误的结果。这怎么可能呢?看到这篇文章,就想得到一个答案:
$selectSQL = "SELECT id from devices where devicetoken = ? and id = ?";
echo "token:".$utoken."\n";
echo "id:".$uid."\n";
$resultId = -33;
if ($stmt->prepare($selectSQL)) {
$stmt->bind_param('ii', $utoken, $uid);
$stmt->execute();
$stmt->bind_result($resultId);
$stmt->fetch();
$stmt->close();
echo $resultId;
if ($resultId == $uid) {
echo "AA";
return true;
} else {
echo "BB";
return false;
}
}
正如我在注释中所述,令牌是一个字符串,您正在绑定中为“integer”传递I
字符串应为“s
”
bind_param('si'
令牌是一个字符串,您正在使用
i
,使用s
。这是一个打字错误的问题。我闻到有人在制定答案;我想不应该有。非常感谢你。这就是问题所在!不客气,杰克,干杯,不过你应该删除它,伊姆霍。正如斯塔克告诉我们的那样,打字错误问题是离题的。