Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php基于数组添加多个参数_Php_Arrays - Fatal编程技术网

php基于数组添加多个参数

php基于数组添加多个参数,php,arrays,Php,Arrays,因此,目前我得到了这段代码,用于准备查询,虽然有效,但效率低下,如果查询变得更大,可能需要扩展。还有更多类似的代码。这些代码的一个示例如下所示 //This function is called when preparing statements to prevent sql-injections(1) //It binds the variables to the prepared statements based on an array. //$stmt is the prepared st

因此,目前我得到了这段代码,用于准备查询,虽然有效,但效率低下,如果查询变得更大,可能需要扩展。还有更多类似的代码。这些代码的一个示例如下所示

//This function is called when preparing statements to prevent sql-injections(1)
//It binds the variables to the prepared statements based on an array.
//$stmt is the prepared statement. $params is an array with the input data.
public function bind($stmt, $params){
    x = count($params);
    $type = $this->getType($params); //simply obtains the types of the params(2)
    if($x == 1){
        $stmt->bind_param($type, $params[0]);
    }elseif($x == 2){
        stmt->bind_param($type, $params[0], $params[1]);
    }elseif($x == 3){
        $stmt->bind_param($type, $params[0], $params[1], $params[2]);
    }else{
        echo "Too much params";      //error if there are more than 3 params.
    }
    return $stmt;
}
引用代码中的链接以获取额外信息,这并不重要,因为它只是一个示例
(1) =
(2) =

如您所见,如果$params包含3个以上的项,那么这将返回一个错误。所以我一直在尝试以更有效的方式解决这个问题,因为添加更多的elseif语句是没有效率的。我尝试了各种各样的循环,但都没有很好的效果。这个问题不仅出现在这个bind_param示例中,也出现在其他函数中

因此,我想要一个简单有效的解决方案,根据数组项的数量向函数调用中添加一定数量的变量,而数组项的数量不仅仅适用于这个示例

public function bind($stmt, $params){
  return call_user_func_array(array($stmt, 'bind_param'), $params);
}

我创建了自己的函数,该函数允许“无限”数量的值,检查数量是否正确,并创建一个类型字符串

但是,这个函数有点不同,因为它要求绑定querystring和对象数组(您的函数需要一个语句)


您正在查找:
call_user_func_数组([$stmt,'bind_param'],$params)。我遇到过它,但我所有实现它的尝试都失败了,所以您能否在我的示例中给出一个如何实现它的示例。此外,如果您确实遇到它并尝试使用它,为什么不在问题中这样说?猜谜可不好玩。至少在这种情况下是这样。如果您使用的是
mysql
,请阅读有关引用和绑定参数@Oscar M的说明:当然,您是对的
PDOStatement::bindParam()
具有并非所有人都期望甚至想要的功能。我只是假设目标是绑定值,而不是参数。在这种情况下,
PDOStatement::bindParam()
将是更好的选择。
function CreateStatement($query, $objects) {
    $questionMarks = substr_count($query, '?');
    if ($questionMarks != count($objects))
        $this->ThrowError('Trying to create statement with invalid data', array(
            'Query' => $query,
            'Objects' => $objects 
        ));


    $statement = $this->prepare($query);

    if (count($objects) != 0) {
        $fields = array();
        $out = array();

        $field_notation = '';
        foreach ($objects as $key => $value) {
            if (is_string($value)) $field_notation .= 's';
            elseif (is_float($value)) $field_notation .= 'd';
            elseif (is_int($value)) $field_notation .= 'i';
            elseif ($value instanceof DateTime) {
                $field_notation .= 's';
                $objects[$key] = $value->format(DateTime::ISO8601);
            }
            else $field_notation .= 'b';
        }

        $fields[] = $field_notation;
        foreach ($objects as &$value) {
            $fields[] = &$value;
        }

        call_user_func_array(array($statement, 'bind_param'), $fields);
    }
    return $statement;
}