根据php手册,您可以通过查询$stmt->error和$stmt->errno来检索任何预准备语句方法中的错误,但是bind_param方法似乎从来没有在错误上设置这两个错误,其他人可以确认这一点吗?或者告诉我我错过了什么?
例如:
echo "Start\n";
$db = new mysqli('localhost','test','xxxxxx','test');
$val = 1;
$st = $db->prepare('insert into tblTest set field1=?');
if($st == false)
{
printf("prepare: %s %d\n",$db->error,$st->errno);
}
$rs = $st->bind_param('is', $val);
if($rs == false)
{
printf("bind_param: %s %d\n",$st->error,$st->errno);
}
$rs = $st->execute();
if($rs == false)
{
printf("execute: %s %d\n",$st->error,$st->errno);
}
$rs = $st->close();
if($rs == false)
{
printf("close: %s %d\n",$st->error,$st->errno);
}
echo "Finish\n";从命令行运行以上命令将显示以下内容:
Start
PHP Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in test.php on line 14
bind_param: 0
execute: No data supplied for parameters in prepared statement 2031
Finish所以php认为这是一个警告,bind_param返回false,但并没有设置错误& errno。execute也失败,并已正确设置错误& errno
这是一个bug吗?
发布于 2012-07-07 19:45:26
MySQLi::error和MySQLi::errno是由关系型数据库管理系统返回的错误代码和消息的容器,而不是设置语句时遇到的错误的容器。对bind_param()的错误调用(参数不足)显然无法与关系数据库通信,因此不会收到来自关系数据库的错误。
根据to the docs的说法,bind_param()在失败时返回一个布尔值FALSE。因此,您需要验证它是否被成功调用。
$rs = $st->bind_param('is', $val);
if ($rs) {
$st->execute();
}https://stackoverflow.com/questions/11374672
复制相似问题