db_pdo::query PHP Method

query() public method

执行查询 返回数据集
public query ( string $str, array $bind = [] ) : mixed
$str string sql指令
$bind array 参数绑定
return mixed
    public function query($str, $bind = array())
    {
        $this->initConnect(false);
        if (!$this->_linkID) {
            return false;
        }
        $this->queryStr = $str;
        if (!empty($bind)) {
            $this->queryStr .= '[ ' . print_r($bind, true) . ' ]';
        }
        //释放前次的查询结果
        if (!empty($this->PDOStatement)) {
            $this->free();
        }
        //N('db_query',1);
        // 记录开始执行时间
        //G('queryStartTime');
        $this->PDOStatement = $this->_linkID->prepare($str);
        if (false === $this->PDOStatement) {
            throw new Exception($this->error());
        }
        // E($this->error());
        // 参数绑定
        $this->bindPdoParam($bind);
        $result = $this->PDOStatement->execute();
        $this->debug();
        if (false === $result) {
            $this->error();
            return false;
        } else {
            return $this->getAll();
        }
    }

Usage Example

 public function trash($tableName, $id, $status)
 {
     $db = new db_pdo();
     $db->query("UPDATE " . $tableName . " SET status = " . $status . " WHERE id = ?", $id);
 }