这真令人困惑,我必须缺少一些简单的东西.我有一个查询,检查要插入的事务是否已经存在,以防止重复.这是代码:
function isaDupe($portableDB, $transactArray)
{
$ref = $transactArray["reference"];
$date = $transactArray["qdate"];
$time = $transactArray["time"];
//prints the query so I can run by hand to test
print "SELECT `counter` FROM transactions WHERE (`reference` = '$ref' AND `qdate` = '$date' AND `time` = '$time') ";
if ($dupeSelectStmt = $portableDB->prepare("SELECT `counter` FROM transactions WHERE (`reference` = ? AND `qdate` = ? AND `time` = ?)"))
{
$dupeSelectStmt->bind_param('sss',$ref, $date, $time);
$dupeSelectStmt->bind_result($counter);
$dupeSelectStmt->execute();
while ($dupeSelectStmt->fetch())
{
break;
}
$numRows = $portableDB->affected_rows;
if ($numRows > 0)
return TRUE;
else if ($numRows == -1)
{
print " ERROR: ";
print_r($portableDB->error);
print_r($dupeSelectStmt->error);
return FALSE;
}
else
return FALSE;
}
}
-如果我通过同一服务器上的Workbench手动运行查询,则会返回24行.
-如果我手动准备,设置和执行语句,这是相同的.
-affected_rows返回-1
-如果我在语句上执行num_rows则相同
-Statement或MySQLi对象上没有存储错误.
-如果将打印内容放入fetch()语句中,它将打印一行数据
-如果将提取的行存储到数组中并计算结果,则为1
-我试过分别使用每个变量运行它,同样的事情.
-同一台服务器(heck,同一MySQLi对象)上的其他查询工作正常.选择,更新和插入.
解决方法:
答案是我忘记了在mysqlistmt :: execute()之后调用mysqlistmt :: store_result.
添加$dupSelectStmt-> store_result()后,我能够调用$dupSelectStmt-> num_rows和$portableDB-> affected_rows,它们都显示了我知道应该看到的24个字符.