Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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 追加未知数量的WHERE参数_Php_Mysql_Arrays - Fatal编程技术网

Php 追加未知数量的WHERE参数

Php 追加未知数量的WHERE参数,php,mysql,arrays,Php,Mysql,Arrays,我正试图找到最合乎逻辑(一如既往)的方法来接受查询中WHERE参数的列表。我遇到了一个常见问题,即如何确定它是否是数组的结尾,我需要添加或不添加另一个“and” 我似乎建议使用end()、reset()、slice、for循环和计数器等,但似乎没有人同意一个答案。我相信end()在我的循环中不起作用。感谢您的帮助。这应该和内爆参数数组一样简单,例如- private function someQuery($where = null){ $this->q = "SELECT stuf

我正试图找到最合乎逻辑(一如既往)的方法来接受查询中WHERE参数的列表。我遇到了一个常见问题,即如何确定它是否是数组的结尾,我需要添加或不添加另一个“and”


我似乎建议使用end()、reset()、slice、for循环和计数器等,但似乎没有人同意一个答案。我相信end()在我的循环中不起作用。感谢您的帮助。

这应该和内爆参数数组一样简单,例如-

private function someQuery($where = null){
    $this->q = "SELECT stuff FROM table";
    if ($where){
        $parameters = array();
        foreach($where as $x){
            $condition = implode(' ', $x); // I'd review this - why are you forming your parameter array the way that you are?
            array_push($parameters, $condition);
        }
        $conditions = implode(' AND ', $parameters);
        $this->q .= ' WHERE ' . $conditions; // only concatenate once
    }
}

收集一个数组,也可以使用<代码>其中1=1 < /Cord>并添加其余。@ tdokiIII如果杰伊的答案如下解,考虑接受答案标记它被解决。
private function someQuery($where = null){
    $this->q = "SELECT stuff FROM table";
    if ($where){
        $parameters = array();
        foreach($where as $x){
            $condition = implode(' ', $x); // I'd review this - why are you forming your parameter array the way that you are?
            array_push($parameters, $condition);
        }
        $conditions = implode(' AND ', $parameters);
        $this->q .= ' WHERE ' . $conditions; // only concatenate once
    }
}