php – mysqli bind_param没有设置$stmt-> error或$stmt-> errno

根据PHP手册,您可以通过询问$stmt->错误和$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没有设置.执行也失败并正确设置错误&错误

这是一个错误吗?

解决方法:

MysqLi :: error和MysqLi :: errno是RDBMS返回给PHP错误代码和消息的容器,而不是设置语句时PHP代码中遇到的错误.对bind_param()错误调用(参数不足)显然不会与RDBMS通信,因此不会从RDBMS接收错误.

根据to the docs,bind_param()在失败时返回一个布尔值FALSE.因此,您需要验证它是否已成功调用.

$rs = $st->bind_param('is', $val);
if ($rs) {
  $st->execute();
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...