准备好AND语句的PDO查询

时间:2016-01-22 01:07:57

标签: php mysql oop pdo

使用OOP PDO数据库包装器,试着这样做,这样我就可以在没有多个查询的情况下为我的查询添加AND语句。出于某种原因,这不起作用但是没有给出< / p>

    public function query($sql, $params = array()){
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)){
        $x = 1;
        if(count($params)){
            foreach($params as $param){
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()){
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        }else{
            $this->_error = true;
        }
    }
    return $this;
}

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

        $field = $where[0];
        $operator = $where[1];
        $value = $where[2];
        if(count($additional) === 4) {
            $condition = $additional[0];
            $field2 = $additional[1];
            $operator2 = $additional[2];
            $value2 = $additional[3];
        }

        if(in_array($operator, $operators)){
            if(count($additional) === 4){
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? {$condition} {$field2} {$operator2} ?";
                if(!$this->query($sql, array($value, $value2))->error()){
                    return $this;
                }
            } else {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
                if(!$this->query($sql, array($value))->error()){
                    return $this;
                }
            }

        }
    }
    return false;
}

public function get($table, $where, $additional = array()){
    return $this->action('SELECT *', $table, $where, $additional);
}

这是用于DB.php的PHP,这是针对该类的。

 public function get($time){
    $field = (is_numeric($time)) ? 'week' : 'day';
    if($field == "day") {
        $week = date("W");
        $data = $this->_db->get("timetable", array($field, '=', $time), array('AND', 'week', '=', $week));
    } else {
        $data = $this->_db->get("timetable", array($field, '=', $time));
    }
    if($data->count()){
        $this->_data = $data->results();
        return true;
    }
    return false;
}

这就是我所说的。

<?php for($i = 0;$i <= 6;$i++){ ?>
        <div id="<?php echo $days[$i];?>" class="tab-pane <?php if ($i == $today) { echo "active"; }?>">
            <?php
            $timetable = new Timetable();
            $timetable->get($days[$i]);
            echo $days[$i];
            if(!empty($timetable->data())) {
                foreach ($timetable->data() as $day) {
                    print_r($day);
                }
            }
            ?>
        </div>
    <?php } ?>

注意 - $days是一周中的日期数组,我每天都会查看并获取当天的数据,但也会查找本周的数据。但是也可以从指定的一周获取信息。

那么它正在创建来自$timetable->get($days[$i])的查询,该查询输入到函数public function get($time)中,然后使用来自使用操作块的第一个代码块的get函数。

我使用的查询:

SELECT * FROM timetable WHERE day = 'Monday' AND week = 3

0 个答案:

没有答案