多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[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); ~~~