如何使用带有数组作为第二个参数的mysqli :: bind_param [英] How to use mysqli::bind_param with an array as the second parameter

查看:330
本文介绍了如何使用带有数组作为第二个参数的mysqli :: bind_param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该查询应该将新用户插入用户"表中

This query is supposed to insert a new user into the 'users' table

$user = DB::getInstance()->insert('users', array(
        'username' => 'jim',
        'password' => 'pass',
        'salt' => 'salt'
       )
);

对应的insert()

Corresponding insert()

public function insert($table, $fields = array())
{
    if (count($fields)) {
        $keys   = array_keys($fields);
        $values = null;
        $x      = 1;
        foreach ($fields as $field) {
            $values .= "?";
            if ($x < count($fields)) {
                $values .= ', ';
            }
            $x++;
        }
        $sql = "INSERT INTO users (`" . implode('`,`', $keys) . "`) VALUES ({$values})";
        echo $sql;
        if($this->queryDB($sql,$fields)){
            echo "its good";
            return true;
        }else{
            echo "bad query";
        }
    }
    return false;
}

尝试将值数组($ param)绑定为bind_param函数的第二个参数

Attempting to bind query an array of values ($param) as the second parameter of bind_param function

    $stmt = $this->mysqli->prepare($pattern);
    $stmt->bind_param("sss", $param);
    $stmt->execute();

此代码不断返回"mysqli_stmt :: bind_param():类型定义字符串中的元素数量与绑定变量的数量不匹配"错误.

This code keeps returning "mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables" error.

我也尝试过call_user_func_array,但仍然遇到相同的错误.我做错了什么阿米?

I have also tried call_user_func_array, but keep getting the same error. What ami I doing wrong?

推荐答案

从PHP 5.6开始,您可以使用splat运算符...

As of PHP 5.6, you can use the splat operator ...

$stmt->bind_param("sss", ...$param);

这篇关于如何使用带有数组作为第二个参数的mysqli :: bind_param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆