修改Function如何获取和查询DB?

时间:2015-05-15 11:31:44

标签: php mysql

以下是我目前的职能:

public function get($table, $where)
{
    if (count($where) === 3) {
        $operators = array(
            '=',
            '>',
            '<',
            '>=',
            '<='
        );

        $field    = $where[0];
        $operator = $where[1];
        $value    = $where[2];

        if (in_array($operator, $operators)) {
            $sql = "SELECT * FROM {$table} WHERE {$field} {$operator} ?";

            if (!$this->query($sql, array(
                $value
            ))->error()) {
                return $this;
            }
        }
    }
    return false;

}

我想修改函数以允许SQL中有更多变量,例如:

SELECT * FROM table WHERE id=2 AND field2 = 'test';

目前正在进行如下查询:

get('table', array('field', '=', 'a'));

我想修改它以进行如下查询:

get('table', array('field1', '=', 'a' AND 'field2', '=', 'b'));

这有意义吗?

我仍然在学习,如果我的代码中存在问题,请告诉我:)

由于

2 个答案:

答案 0 :(得分:1)

<?php
function _get($table, $wheres, $operatorBtwnWheres = "AND")
{
    $conditions = array();
    $operators = array('=', '>', '<', '>=', '<=' );

    foreach($wheres as $where) {
        if (count($where) === 3) {
            $field    = $where[0];
            $operator = $where[1];
            $value    = $where[2];

            if (in_array($operator, $operators)) 
                $conditions[] = " $field $operator $value ";
        }
    }
    $w = implode($operatorBtwnWheres, $conditions);
    return "SELECT * FROM $table WHERE $w;";
}

echo _get('yourTable', array( array('field1', '=', "'a'"), array('field2', '=', "'b'") ));
// output: SELECT * FROM yourTable WHERE field1 = 'a' AND field2 = 'b' ;

echo _get(
    'yourTable2', 
    array( array('field3', '<', "10"), array('field4', '>', "8"), array('field5', '<=', "0") ),
    "OR");
// output: SELECT * FROM yourTable2 WHERE field3 < 10 OR field4 > 8 OR field5 <= 0 ;
?>

答案 1 :(得分:0)

<?php
    function get($table_name, $where_array=array(), $where_logical_operators=array() ) {
         $operators = array('=',
                            '>',
                            '<',
                            '>=',
                            '<=')
                            ;
        $where_string = ' WHERE 1 = 1 ';
        if ( ceil(count($where_array)/2) != count($where_logical_operators)) {
            return "The number of logical operators between conditions must be equal to the number of conditions/2 + 1.";
        }
        // After WHERE 1=1 follows AND
        array_unshift($where_logical_operators, "AND");

        if ( count($where_array)>0 ) {
            foreach($where_array as $array_no => $val0){
                if ( count($where_array[$array_no]) === 3 && in_array($where_array[$array_no][1], $operators)){
                    $aux = array();

                    $aux['field'] = $where_array[$array_no][0];
                    $aux['operator'] = $where_array[$array_no][1];
                    $aux['value'] = $where_array[$array_no][2];

                    $where_string .= " {$where_logical_operators[$array_no]} {$aux['field']} {$aux['operator']} {$aux['value']}";
                }

            }
        }
        return "SELECT * FROM {$table_name} {$where_string}"; 
    }


    echo get('my_table', array( array('field_1', '=', 'value_of_field_1'), array('field_2', '<=', 'value_of_field_2'), array('field_3', '>=', 'value_of_field_3') ), array('OR', 'AND') );
    // Outputs
    // SELECT * FROM my_table WHERE 1 = 1 AND field_1 = value_of_field_1 OR field_2 <= value_of_field_2 AND field_3 >= value_of_field_3
?>