Bind Param with array of parameters(将参数与参数数组绑定)
问题描述
我有一个函数可以做到这一点:
I have a function that does this:
function registerUser($firstName, $lastName, $address, $postcode, $email, $password)
{
$params = array($firstName, $lastName, $address, $postcode, $email, $password);
$result = $this->db->bind("INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?)", 'ssssss', $params);
}
发送到我的数据库类,它执行以下操作:
Which sends off to my database class, which does this:
public function bind($query, $type, $params)
{
$this->query = $query;
$stmt = $this->mysqli->prepare($this->query);
$stmt->bind_param($type, $param);
$stmt->execute;
}
问题是这行不通.
我希望做的是获取 $params 列表并让它在 $type 之后列出它们,这样查询将类似于:>
What I was hoping to do, was to take the $params list and have it list them after the $type, so that the query would resemble:
$stmt->bind_param('ssssss', $firstName, $lastName, $address, $postcode, $email, $password);
但显然我的做法是错误的.
But obviously I'm going about it the wrong way.
有没有办法让数组...转换成一个列表,以便在 bind_param 查询阶段打印出来?
is there a way to make the array...transform as it were, into a list to be printed out at the bind_param query stage?
推荐答案
call_user_func_array使用参数数组调用回调"
call_user_func_array "Call a callback with an array of parameters"
call_user_func_array(array($stmt, "bind_param"), array_merge(array($type), $params));
应该做的工作
UPDATE:您还必须更改您的 params 数组:
UPDATE: you have also to change your params array:
$params = array(&$firstName, &$lastName, &$address, &$postcode, &$email, &$password);
as mysqli_stmt::bind_param 期待第二个和以下参数的引用.
as mysqli_stmt::bind_param expects the second and the following parameters by reference.
您的查询似乎是错误的.也许你的字段比变量少.做:
Your query seems to be wrong. Maybe you have less fields than you have variables there. Do:
"INSERT INTO Users (field1, field2, field3, field4, field5, field6) VALUES (?, ?, ?, ?, ?, ?)"
用正确的名称替换字段的名称
where you replace the name of the fields by the correct names
这篇关于将参数与参数数组绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将参数与参数数组绑定
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
