在PHP中通过引用传递导致问题 [英] Passing by reference in PHP causing issues

查看:80
本文介绍了在PHP中通过引用传递导致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





事先,我有一个允许用户指定mysqli连接的方法,带有'?'的查询字符串以避免SQL注入,以及一系列参数。然后它会动态绑定params并将结果输出为关联数组。



现在我已经将参数绑定部分移动到一个单独的方法中,因为我是为它添加更多实现。现在的问题是:



Hi,

Beforehand, I had a method which allows the user to specify a mysqli connection, a query string with '?' to avoid SQL injection, and an array of parameters. It would then dynamically bind the params and output the result as an associative array.

Now I've moved the parameter binding part out into a separate method as I'm adding more implementations for it. The issue now is:

call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object



这是方法:


This is the method:

private static function bind_dynamic_params(&$stmt, $params)
        {
                // generate the params type string
                $param_types = str_repeat('s', count($params));

                // set the type string as the first parameter
                $param_bind_names[] = $param_types;

                // iterate through params
                for($i = 0; $i < count($params); $i++)
                {
                        // Create a variable name
                        $param_bind_name = 'bind' . $i;

                        // add the parameter to the variable
                        $$param_bind_name = $params[$i];

                        // associate the variable as an element in the array
                        $param_bind_names[] = &$$param_bind_name;
                }

                // bind params
                call_user_func_array(array($stmt, 'bind_param'), $param_bind_names);
        }



我想通过引用传递$ stmt对象,因此可以在其上调用 call_user_func_array ,而原始方法可以使用这个改变的对象。



我试图这样做的方式是:



1. $ conn = new mysqli(...); $ stmt = $ conn-> prepare($ query);

2. bind_dynamic_params($ stmt,$ params);

3.现在继续使用$ stmt,例如as - > execute()或 - > close()。



我尝试过:



通过引用传递,在线查找大约半小时


I'm looking to pass the $stmt object by reference so call_user_func_array can be called on it, while the original method can use this altered object.

The way I'm trying to do this is:

1. $conn = new mysqli(...); $stmt = $conn->prepare($query);
2. bind_dynamic_params($stmt, $params);
3. Continue using $stmt now, such as ->execute() or ->close().

What I have tried:

Passing by reference, looking online for around half an hour

推荐答案

stmt,


params)
{
// 生成参数类型字符串
params) { // generate the params type string


param_types = str_repeat(' s',count(
param_types = str_repeat('s', count(


这篇关于在PHP中通过引用传递导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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