Mysql、SqlServer、PgSQL、Sqlite等数据库的支持。
a、配置文件目录
C:\AppServ\www\tp5\application\database.php
b、如何配置
return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'yzmedu',
// 用户名
'username' => 'root',
// 密码
'password' => '123456789',
// 端口
'hostport' => '3306',
];
c、如何使用
// 实例化系统数据库类
$DB=new Db;
// 查询数据
$data=$DB::table("user")->select();
// 使用sql语句
$data=$DB::query("select * from user");
1、增加
# 返回值 影响行数
$data=Db::execute("insert into user value(null,'user1','123','18')");
$data=Db::execute("insert into user value(null,?,?,?)",['user2','456','20']);
$data=Db::execute("insert into user value(null,:name,:pass,:age)",['name'=>"user3","pass"=>'678','age'=>25]);
2、修改
#返回值 影响行数
$data=Db::execute("update user set age='20' where id=9");
3、删除
#返回值 影响行数
$data=Db::execute("delete from user where id=10");
$data=Db::execute("delete from user where id>?",[15]);
$data=Db::execute("delete from user where id>:id",["id"=>10]);
4、查看
$data=Db::query("select * from user");
$data=Db::query("select * from user where id >=? and id<=?",[5,8]);
5、获取指定sql语句
Db::getLastSql();
1、table方法查询数据
// 查询所有数据
// SELECT * FROM `user`
$data=Db::table("user")->select();
// 查询一条数据
// SELECT * FROM `user` LIMIT 1
$data=Db::table("user")->find();
2、name方法查询数据
// 与配置文件有关
// name 会自动添加配置文件中的表前缀
$data=Db::name("user")->select();
$data=Db::name("user")->find();
3、助手函数
# SELECT * FROM `user`
$data=db("user")->select();
# SELECT * FROM `user` LIMIT 1
$data=db("user")->find();
4、where条件匹配
// SELECT * FROM `user` WHERE `id` > 25
$data=Db::table("user")->where("id",">",25)->select();
// SELECT * FROM `user` WHERE ( `id` > 25 AND `id` < 28 )
$data=Db::table("user")->where("id",">",25)->where("id","<",28)->select();
// SELECT * FROM `user` WHERE `name` LIKE '%user1%'
$data=Db::table("user")->where("name","like","%user1%")->select();
// SELECT * FROM `user` WHERE `name` = 'user3' AND `pass` = 'qwe'
$data=Db::table("user")->where("name","user3")->where("pass",'qwe')->select();
// SELECT * FROM `user` WHERE ( id > 25 and id <28 )
$data=Db::table("user")->where("id > 25 and id <28")->select();
// SELECT * FROM `user` WHERE `id` > 25 AND `name` = 'user10'
$data=Db::table("user")->where(["id"=>[">",25],"name"=>'user10'])->select();
// SELECT * FROM `user` WHERE ( `id` > 25 AND `id` < 28 )
$data=Db::table("user")->where(["id"=>[">",25]])->where(['id'=>["<",28]])->select();
5、whereOr 或者匹配
// SELECT * FROM `user` WHERE `id` <= 23 OR `id` >= 28
$data=Db::table("user")->where("id","<=",23)->whereOr("id",">=",28)->select();
// SELECT * FROM `user` WHERE `name` LIKE '%user1%' OR `name` LIKE '%user2%'
$data=Db::table("user")->where("name","like","%user1%")->whereOr("name","like","%user2%")->select();
6、limit 截取数据
// SELECT * FROM `user` LIMIT 5
$data=Db::table("user")->limit(5)->select();
// SELECT * FROM `user` LIMIT 5,5
$data=Db::table("user")->limit(5,5)->select();
7、Order 排序
// SELECT * FROM `user` ORDER BY `id`
$data=Db::table("user")->order("id")->select();
// SELECT * FROM `user` ORDER BY `id` desc
$data=Db::table("user")->order("id","desc")->select();
8、field 设置查询字段
// 设置查询字段
// SELECT `name`,`pass` FROM `user`
$data=Db::table("user")->field("name,pass")->select();
$data=Db::table("user")->field(['name','pass'])->select();
// 起别名
// SELECT name uname,`pass` FROM `user`
$data=Db::table("user")->field("name uname,pass")->select();
$data=Db::table("user")->field(['name'=>"uname",'pass'])->select();
// sql 的系统函数
// SELECT count(*) as tot FROM `user`
$data=Db::table("user")->field("count(*) as tot")->select();
$data=Db::table("user")->field(['count(*)'=>"tot"])->select();
// 排除字段
// SELECT `id`,`age` FROM `user`
$data=Db::table("user")->field("name,pass",true)->select();
$data=Db::table("user")->field(["name",'pass'],true)->select();
9、Page 实现分页效果
// SELECT * FROM `user` LIMIT 5,5
$data=Db::table("user")->page("1,5")->select();
10、Group分组聚合
// SELECT `pass`,count(*) tot FROM `user` GROUP BY pass
$data=Db::table("user")->field("pass,count(*) tot")->group("pass")->select();
11、having 过滤
// 只能结合分组使用
// SELECT `pass`,count(*) tot FROM `user` GROUP BY pass HAVING tot >=2
$data=Db::table("user")->field("pass,count(*) tot")->having("tot >=2")->group("pass")->select();
12、多表查询
// select goods.*,type.name tname from type,goods where goods.cid=type.id
$data=Db::query("select goods.*,type.name tname from type,goods where goods.cid=type.id");
// 内联实现数据库链接
// SELECT `goods`.*,type.name tname FROM `goods` INNER JOIN `type` `type` ON `goods`.`cid`=`type`.`id`
$data=Db::table("goods")->field("goods.*,type.name tname")->join("type","goods.cid=type.id")->select();
// 右链接
// SELECT `goods`.*,type.name tname FROM `goods` RIGHT JOIN `type` `type` ON `goods`.`cid`=`type`.`id`
$data=Db::table("goods")->field("goods.*,type.name tname")->join("type","goods.cid=type.id",'right')->select();
// 左链接
$data=Db::table("goods")->field("goods.*,type.name tname")->join("type","goods.cid=type.id",'left')->select();
13、别名使用-给表起别名
// SELECT `g`.*,t.name tname FROM `goods` `g` LEFT JOIN `type` `t` ON `g`.`cid`=`t`.`id`
$data=Db::table("goods")->alias("g")->field("g.*,t.name tname")->join("type t","g.cid=t.id",'left')->select();
14、union集合
// SELECT `name` FROM `user` UNION select name from goods
$data=Db::field("name")->table("user")->union("select name from goods")->select();
15、参数绑定(bind) 为了防止sql注入
# DELETE FROM `user` WHERE `id` = 25
$id=input("id");
$data=Db::table("user")->where("id",":id")->bind(["id"=>[$id,\PDO::PARAM_INT]])->delete();
16、数据统计
// SELECT MAX(age) AS tp_max FROM `user` LIMIT 1
$data=Db::table("user")->max("age");
// SELECT MIN(age) AS tp_min FROM `user` LIMIT 1
$data=Db::table("user")->min("age");
// SELECT AVG(age) AS tp_avg FROM `user` LIMIT 1
$data=Db::table("user")->avg("age");
// SELECT SUM(age) AS tp_sum FROM `user` LIMIT 1
$data=Db::table("user")->sum("age");
// SELECT COUNT(age) AS tp_count FROM `user` LIMIT 1
$data=Db::table("user")->count("age");