开发者

Need opinions on variable query build up

I'm trying to build up some functions to variably build up queries from arrays. And i need to know whether i'm actually doing it correctly. Right now i'm wondering if this function to build a variable "WHERE" is okay, or should be improved.

Any opinions / tips are appreciated.

private function buildWhereFromArray($var, $array, $count)
    {
        $where = '(';
        for($i = 0; $i <= ($count - 1); $i++)
        {
            if(!is_numeric($array[$i]))
            {
                return false;
            }
            $where .= '('.$var.' = '.$array[$i].')';
            if($i != ($typeCount - 1))
            {
                $where .= ' AND ';
            }
        }
        开发者_运维知识库$where .= ')';
        return $where;
    }

The variables should be obvious.


If you want to limit a resultset to, say, foo is a, b or c, you can use the mysql IN() function.

$where = "WHERE `{$var}` IN('a', 'b', 'c')";


AND seems to be strange. Lets say $array[0]!=$array[1]

How it is possible to have $var=$array[0] AND $var=$array[1]

Need you OR?

Besides you can use it some shorter

implode(' AND ',array_filter(array_map(function($elem){
    if(!is_numeric($elem))
        return ''; //will be filtered
    return '('.$var.' = '.$elem.')';
},$arr)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜