MySQLI 使用 call_user_func_array 绑定参数

MySQLI binding params using call_user_func_array(MySQLI 使用 call_user_func_array 绑定参数)
本文介绍了MySQLI 使用 call_user_func_array 绑定参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

请看下面我的代码.我试图将一组参数绑定到我准备好的语句.我一直在网上环顾四周,可以看到我必须使用 call_user_func_array 但无法让它工作.我得到的错误是:第一个参数应该是一个有效的回调,给出了'Array'"我可能是错的,但我假设第一个参数可以是一个数组,并且这个错误消息可能具有误导性.我认为问题在于我的阵列在某种程度上有问题.谁能看到我做错了什么?谢谢.

$type = array("s", "s");$param = array("string1","anotherstring");$stmt = $SQLConnection->prepare("INSERT INTO mytable (comp, addl) VALUES (?,?)");$params = array_merge($type, $param);call_user_func_array(array(&$stmt, 'bind_param'), $params);$SQLConnection->execute();

解决方案

我不知道你为什么必须使用 call_user_func_array,但那是另一回事了.

在我看来,唯一可能出错的是您使用了对对象的引用.假设您使用的是 PHP 5.*,则没有必要:

call_user_func_array(array($stmt, 'bind_param'), $params);

Please see below my code. I am attempting to bind an array of paramenters to my prepared statement. I've been looking around on the web and can see I have to use call_user_func_array but cannot get it to work. The error I get is: "First argument is expected to be a valid callback, 'Array' was given" I may be wrong but I'm assuming the first argument can be an an array and perhaps this error message is misleading. I think the issue is that my array is in someway at fault. Can anyone see what I am doing wrong? Thanks.

$type = array("s", "s");
$param = array("string1","anotherstring");

$stmt = $SQLConnection->prepare("INSERT INTO mytable (comp, addl) VALUES (?,?)");

$params = array_merge($type, $param);

call_user_func_array(array(&$stmt, 'bind_param'), $params);
$SQLConnection->execute();

解决方案

I wouldn't know why you have to use call_user_func_array, but that's another story.

The only thing that could be wrong in my eyes is that you are using a reference to the object. Assuming you're using PHP 5.*, that is not necessary:

call_user_func_array(array($stmt, 'bind_param'), $params);

这篇关于MySQLI 使用 call_user_func_array 绑定参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?(警告:mysqli_query() 需要至少 2 个参数,1 个给定.什么?)
INSERT query produces quot;Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean givenquot;(INSERT 查询产生“警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,给出布尔值;)
prepared statements - are they necessary(准备好的陈述 - 它们是否必要)
Do I need to escape my variables if I use MySQLi prepared statements?(如果我使用 MySQLi 准备好的语句,是否需要转义我的变量?)
Properly Escaping with MySQLI | query over prepared statements(使用 MySQLI 正确转义 |查询准备好的语句)
Is it possible to use mysqli_fetch_object with a prepared statement(是否可以将 mysqli_fetch_object 与准备好的语句一起使用)