PHP准备好的语句返回-1 [英] PHP Prepared Statement Returns -1
问题描述
我已经使用准备好的语句已有一段时间了,而且我从未遇到任何问题.
I've been using prepared statements for a little while now and I've never had any problems.
现在我正在尝试:
$sql="SELECT PhotoID,Caption FROM Photos WHERE EntityID=? AND TypeID=? LIMIT ?,?";
$iDB = new mysqliDB(); // Extends mysqli
$stmt = $iDB->prepare($sql);
$stmt->bind_param('iiii',$entityID,$typeID,$minRange,$maxRange);
$stmt->execute();
$stmt->bind_result($photoID,$caption);
echo("Affected={$stmt->affected_rows}");
这将打印-1.我已经进行了三次测试,设置bindParam中的所有4个值都已设置,并且将带有相应值的sql查询粘贴到myAdmin中后即可正常工作.
This prints -1. I have triple tested that all 4 values in bindParam are set, and that the sql query works when pasted into myAdmin with the respective values.
您知道是什么原因造成的吗?
Any idea what may be causing this?
:我在网上找到了答案,显然我需要使用$ stmt-> store_result();.执行后..但我不确定现在是否需要它.
: I found the answer online, apparently I need to use $stmt->store_result(); after executing.. but I am not sure it's needed now and never before..
推荐答案
每个mysqli函数/方法可能失败.根据扩展class mysqli
的方式,您可能必须测试每个返回值.如果方法返回false
,则会发生错误,并且错误消息将存储在mysqli或语句对象的属性中.
Each mysqli function/method can fail. Depending on how you have extended class mysqli
you probably have to test each and every return value. If a method return false
an error occurred and the error message is stored in a property of either the mysqli or the statement object.
$sql="SELECT PhotoID,Caption FROM Photos WHERE EntityID=? AND TypeID=? LIMIT ?,?";
$iDB = new mysqliDB(); // Extends mysqli
if ($iDB->connect_error) {
printf('connect error (%d) %s', $iDB->connect_errno, htmlspecialchars($iDB->connect_error));
die;
}
$stmt = $iDB->prepare($sql);
if ( false===$stmt ) {
printf('prepare failed: %s', htmlspecialchars($iDB->error));
die;
}
$rc = $stmt->bind_param('iiii',$entityID,$typeID,$minRange,$maxRange);
if ( false===$rc ) {
printf('bind_param failed: %s', htmlspecialchars($stmt->error));
die;
}
$rc = $stmt->execute();
if ( false===$rc ) {
printf('execute failed: %s', htmlspecialchars($stmt->error));
die;
}
$rc = $stmt->bind_result($photoID,$caption);
if ( false===$rc ) {
printf('bind_result failed: %s', htmlspecialchars($stmt->error));
die;
}
// echo("Affected={$stmt->affected_rows}");
这篇关于PHP准备好的语句返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!