mysqli bind_param没有设置$ stmt->错误或$ stmt-> errno [英] mysqli bind_param does not set $stmt->error or $stmt->errno
问题描述
根据php手册,您可以通过查询$ stmt-> error和$ stmt-> errno来检索任何准备好的语句方法中的错误,但是bind_param方法似乎永远不会将它们设置为错误,还有其他人可以确认吗?还是告诉我我想念的是什么?
According to the php manual you can retrieve errors in any prepared statement method by interrogating $stmt->error and $stmt->errno however the bind_param method never seems to set these on an error, can anyone else confirm this? or tell me what I am missing please?
例如:
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";
从命令行运行以上显示:
Running the above from the command line displays:
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
So php is seeing this as a Warning, bind_param is returning false, but error & errno are not set. execute is also failing and has correctly set error & errno
这是一个错误吗?
推荐答案
MySQLi::error
和MySQLi::errno
是RDBMS返回给PHP的错误代码和消息的容器,而 not 则是错误代码和消息的容器.设置语句时的PHP代码.对bind_param()
的错误调用(参数不足)显然不会与RDBMS通信,因此不会从RDBMS接收错误.
MySQLi::error
and MySQLi::errno
are containers for error codes and messages returned by the RDBMS to PHP, not for errors encountered in the PHP code when setting up the statement. An incorrect call to bind_param()
(with insufficient parameters) does apparently not communicate with the RDBMS, and thus won't receive its error from the RDBMS.
根据文档 ,bind_param()
在以下位置返回布尔值FALSE
失败.因此,您将需要验证它是否被成功调用.
According to the docs, bind_param()
returns a boolean FALSE
on failure. So you will need to verify that it was called successfully.
$rs = $st->bind_param('is', $val);
if ($rs) {
$st->execute();
}
这篇关于mysqli bind_param没有设置$ stmt->错误或$ stmt-> errno的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!