[TOC]
### 生成查詢語句
~~~php
$db = \Lying::$maker->db;
//直接語句
$statement = $db->prepare('select * from user where id=1');
//參數綁定,問號形式的占位符和:id形式的占位符不能混用
$statement = $db->prepare('select * from user where id=:id', [':id'=>1]);
$statement = $db->prepare('select * from user where id=?', [1=>1]); //不推薦這樣使用
$statement = $db->prepare('select * from user where id=?')->bindValue(1, 1); //不推薦這樣使用
$statement = $db->prepare('select * from user where id=:id')->bindValue(':id', 1);
$id = 1;
$statement = $db->prepare('select * from user where id=:id')->bindParam(':id', $id);
//設置參數數據類型
$statement = $db->prepare('select * from user where id=:id')->bindValue(':id', 1, \PDO::PARAM_INT);
$id = 1;
$statement = $db->prepare('select * from user where id=:id')->bindParam(':id', $id, \PDO::PARAM_INT);
//強制使用主庫
$statement = $db->prepare('select * from user where id=1')->useMaster(true);
//取消強制使用主庫
$statement = $db->prepare('select * from user where id=1')->useMaster(false);
//預處理表名
$statement = $db->prepare('select * from {{user}} where id=1');
//select * from `user` where id=1
$statement = $db->prepare('select * from {{lying.user}} where id=1');
//select * from `lying`.`user` where id=1
$statement = $db->prepare('select * from {{%user}} where id=1');
//select * from `prefix_user` where id=1 注:prefix_ 為配置文件設置的表前綴
//預處理字段名(一般用于字段和sql關鍵字一樣的時候,給字段加上反引號)
$statement = $db->prepare('select [[username]] from user where id=1');
//select `username` from user where id=1
$statement = $db->prepare('select [[u.username]] from user as u where id=1');
//select `u`.`username` from user where id=1
~~~
### 獲取生成的語句
~~~php
$db = \Lying::$maker->db;
//直接語句
$statement = $db->prepare('select * from user where id=1');
//獲取語句
$sql = $statement->getSql();
~~~
### 執行語句
~~~php
$db = \Lying::$maker->db;
$statement = $db->prepare('update user set money=1000 where id=1');
//執行sql語句并返回受影響的行數
$rows = $statement->exec();
~~~
### 查詢結果
~~~php
$db = \Lying::$maker->db;
$statement = $db->prepare('select * from user');
//返回結果集中的一條記錄
$res = $statement->one(); //返回數組
$res = $statement->one(true); //返回對象
//返回所有查詢結果的數組
$res = $statement->all(); //返回二維數組
$res = $statement->all(true); //返回結果對象數組
//從結果集中的下一行返回單獨的一個字段值,查詢結果為標量
$res = $statement->scalar(); //第一列
$res = $statement->scalar(1); //第二列
//從結果集中的取出第N列的值
$res = $statement->column(); //第一列
$res = $statement->column(1); //第二列
~~~
### 函數參考
~~~php
/**
* 使用主庫
* @param bool $useMaster 是否使用主庫,默認true
* @return $this
*/
public function useMaster($useMaster = true);
~~~
* * * * *
~~~php
/**
* 獲取SQL語句,這個語句只是參數替換的,不一定是PDO執行的語句
* @param bool $raw 是否獲取不替換參數的語句,默認否
* @param array $params 引用返回綁定的參數
* @return string 返回SQL語句
*/
public function getSql($raw = false, &$params = []);
~~~
* * * * *
~~~php
/**
* 綁定值到語句
* @param string|int $name 參數名
* @param mixed $value 參數值
* @param int $dataType PDO數據類型,不傳的話自動獲取
* @return $this
*/
public function bindValue($name, $value, $dataType = null);
~~~
* * * * *
~~~php
/**
* 綁定變量到語句;注意:使用變量綁定后已經生成預處理語句,所以再使用useMaster()已經不再有效果
* @param string|int $name 參數名
* @param mixed $value 變量
* @param int $dataType PDO數據類型
* @param int $length 數據類型長度
* @param mixed $driverOptions 特殊的參數
* @return $this
*/
public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null);
~~~
* * * * *
~~~php
/**
* 執行SQL語句
* @return int 返回受影響的行數
*/
public function exec();
~~~
* * * * *
~~~php
/**
* 返回結果集中的一條記錄
* @param bool $obj 是否返回對象(默認否返回關聯數組)
* @param string $class 要實例化的對象,不寫默認為\stdClass
* @return mixed 成功返回查詢結果,失敗返回false
*/
public function one($obj = false, $class = null);
~~~
* * * * *
~~~php
/**
* 返回所有查詢結果的數組
* @param bool $obj 是否返回對象(默認否返回關聯數組)
* @param string $class 要實例化的對象,不寫默認為\stdClass
* @return mixed 成功返回查詢結果,失敗返回false
*/
public function all($obj = false, $class = null);
~~~
* * * * *
~~~php
/**
* 從結果集中的下一行返回單獨的一個字段值,查詢結果為標量
* @param int $columnNumber 你想從行里取回的列的索引數字,以0開始
* @return mixed 返回查詢結果,查詢結果為標量
*/
public function scalar($columnNumber = 0);
~~~
* * * * *
~~~php
/**
* 從結果集中的取出第N列的值
* @param int $columnNumber 你想從行里取回的列的索引數字,以0開始
* @return mixed 返回查詢結果
*/
public function column($columnNumber = 0);
~~~
- 序言
- 更新日志
- 安裝
- 規范
- 常量
- 配置
- 自動加載
- MVC
- 模塊
- 控制器
- 模型
- 視圖
- php原生模板
- 模板引擎
- 變量輸出
- 模板注釋
- 模板繼承
- 模板引用
- 流程控制
- 原樣輸出
- 服務組件
- Hook組件
- Request組件
- Router組件
- Cookie組件
- Encrypter組件
- Dispatch組件
- Response組件
- View組件
- Session組件
- Helper組件
- 數據分頁
- 數據驗證
- Logger組件
- Cache組件
- Redis組件
- Connection組件
- 執行sql語句
- 查詢生成器
- 查詢方法詳解
- Schema
- Captcha組件
- CLI
- CLI工具
- 事件
- 類事件
- 實例事件
- 全局事件
- 助手函數
- 擴展
- 異常
- 部署
- Apache
- Nginx
- IIS
- 虛擬主機