Think PHP:
多例/多应用模式:
1.创建项目目录;
2.把controller目录放入项目目录中;
3.config目录app.php修改配置项 auto.multi.app改为true;
4.控制器命名空间填上上新建项目目录名
访问: 域名/index.php/项目名/文件名/函数名;
调试模式: 开启.env文件将APP_DEBUG = 改为true
Trace调试辅助工具:开启全局中间件 middleware.php 增加 think\middleware\TraceDebug
数据库增删改查语句:
表达式:
实例
<?php //访问地址:域名/index.php/sql/index/函数名 namespace app\sql\controller; use app\BaseController; use think\facade\Config; use think\facade\Db; class index extends BaseController { //query执行原生mysql语句 查询 public function a() { $data = Db::query("select * from staff"); foreach($data as $v) { echo "<li>{$v['staff_id']}:{$v['name']}---{$v['age']}---{$v['position']}</li>"; } } //execute执行原生mysql语句 新增 更改 public function b() { //增加 $data = Db::execute("insert into staff set `name`='一灯大师',`age`='1',`sex`='1' ,`position`='驸马爷',`mobile`='13211111115'"); if ($data > 0) { echo '成功添加'.$data.'条记录'; } //更改 // $data = Db::execute("update staff set `name`='hongjicaho' where `staff_id`=2 "); // if ($data > 0) { // echo '成功添加'.$data.'条记录'; // } } public function c() { // 单条查询 // $data = Db::table('staff')->where('staff_id','>',4)->find(); // echo "<li>{$data['staff_id']}:{$data['name']}---{$data['age']}---{$data['position']}</li>"; // 多条查询 $data = Db::table('staff')->select(); foreach($data as $v) { echo "<li>{$v['staff_id']}:{$v['name']}---{$v['age']}---{$v['position']}</li>"; } } public function d() { //查询字段的值 $data = Db::table('staff')->where('staff_id',6)->value('name,mobile'); print_r($data); } public function e() { //查询一列的值 $data = Db::table('staff')->where('staff_id','>',5)->column('name','staff_id'); //column 第一个为值,第二个为键名; // print_r($data); foreach ($data as $staff_id => $name) { echo $staff_id.'='. $name .'<br>'; } } public function f() { //添加 $data = ['name'=>'洪大师','age'=>18,'sex'=>1,'position'=>'抓鬼大师','mobile'=>'15626475734','hiredate'=>time()]; $insert = Db::table('staff')->insert($data); if ($insert > 0) { echo '成功添加'.$insert.'条记录'; } } public function g() { // 添加或修改save 自动判断是新增还是更新数据(以写入数据中是否存在主键数据为依据)。 // $data = ['name'=>'洪大师','age'=>18,'sex'=>1,'position'=>'抓鬼大师','mobile'=>'15626475734','hiredate'=>time()]; // $save = Db::table('staff')->save($data); // if ($save > 0) { // echo '成功添加'.$save.'条记录'; // } //修改 $data = ['staff_id'=>51,'name'=>'李大师','age'=>18,'sex'=>1,'position'=>'抓鬼大师','mobile'=>'15626475734','hiredate'=>time()]; $save = Db::table('staff')->save($data); if ($save > 0) { echo '成功修改'.$save.'条记录'; } } public function h() { //添加数据成功返回添加数据的自增主键 $data = ['name'=>'蔡徐坤','age'=>38,'sex'=>1,'position'=>'抓鬼大师','mobile'=>'15626475734','hiredate'=>time()]; $insertGetId = Db::table('staff')->insertGetId($data); if ($insertGetId > 0) { echo '成功添加数据,主键:'.$insertGetId; } } public function i() { $data = [ ['name'=>'蔡徐坤1','age'=>38,'sex'=>1,'position'=>'抓鬼大师1','mobile'=>'15626475731','hiredate'=>time()], ['name'=>'蔡徐坤2','age'=>38,'sex'=>1,'position'=>'抓鬼大师2','mobile'=>'15626475732','hiredate'=>time()], ['name'=>'蔡徐坤3','age'=>38,'sex'=>1,'position'=>'抓鬼大师3','mobile'=>'15626475733','hiredate'=>time()], ['name'=>'蔡徐坤4','age'=>38,'sex'=>1,'position'=>'抓鬼大师4','mobile'=>'15626475734','hiredate'=>time()], ['name'=>'蔡徐坤5','age'=>38,'sex'=>1,'position'=>'抓鬼大师5','mobile'=>'15626475735','hiredate'=>time()], ]; $insertAll = Db::table('staff')->insertAll($data); if ($insertAll > 0) { echo '成功添加数据,主键:'.$insertAll; } } public function j() { //修改 update 添加或修改 save $data = ['name'=>'didi','mobile'=>'13049050600']; $update = Db::table('staff')->where('staff_id',10)->update($data); if ($update > 0) { echo '成功修改'.$update.'条记录'; }else { echo '数据没有修改'; } } public function k() { //自增inc 自减dec //第一个值为字段名,第二个值为自增或自减多少值 $inc = Db::table('staff')->where('staff_id',55)->inc('age',10)->update(); print_r($inc); $dec = Db::table('staff')->where('staff_id',55)->inc('age',10)->update(); } public function l() { //根据条件删除 // $delete = Db::table('staff')->where('staff_id',62)->delete(); //根据主键删除 $delete = Db::table('staff')->delete(61); //删除整表数据 // $delete = Db::name('staff')->delete(true); //软删除 // $delete = Db::name('user')->useSoftDelete('status',2)->delete(); } public function m() { //模糊查询 like/whereLike //// not like /whereNotLike // $like = Db::table('staff')->where('name','like','蔡徐%')->select(); //快捷语法 // $like = Db::table('staff')->wherelike('name','蔡徐%')->select(); $like =Db::table('staff')->whereNotLike('name','蔡徐%')->select(); // print_r($like); foreach($like as $v) { echo "<li>{$v['staff_id']}:{$v['name']}---{$v['age']}---{$v['position']}</li>"; } } public function n() { //区间查询 between 区间外 NOTbetween // $select = Db::table('staff')->where('staff_id','between','10,20')->select(); //快捷语法 $select = Db::table('staff')->whereBetween('staff_id','10,20')->select(); //区间外查询 // $select = Db::table('staff')->where('staff_id','not between','10,20')->select(); //快捷语法 // $select = Db::table('staff')->whereNotBetween('staff_id','10,20')->select(); foreach($select as $v) { echo "<li>{$v['staff_id']}:{$v['name']}---{$v['age']}---{$v['position']}</li>"; } } public function o() { //IN查询/not in 查询 // $select = Db::table('staff')->where('staff_id','in','10,20,35')->select(); //快捷语法 $select = Db::table('staff')->whereIn('staff_id','10,20,35')->select(); //not in查询 // $select = Db::table('staff')->where('staff_id','not in','10,20,35')->select(); //快捷语法 // $select = Db::table('staff')->whereNotIn('staff_id','10,20,35')->select(); foreach($select as $v) { echo "<li>{$v['staff_id']}:{$v['name']}---{$v['age']}---{$v['position']}</li>"; } } public function p() { //null查询 // $select = Db::table('staff')->where('hiredate','null')->select(); //快捷语法 // $select = Db::table('staff')->whereNull('hiredate')->select(); // foreach($select as $v) // { // echo "<li>{$v['staff_id']}:{$v['name']}---{$v['age']}---{$v['position']}</li>"; // } // not null查询 // $select = Db::table('staff')->where('hiredate','not null')->select(); //快捷语法 // $select = Db::table('staff')->whereNotNull('hiredate')->select(); foreach($select as $v) { echo "<li>{$v['staff_id']}:{$v['name']}---{$v['age']}---{$v['position']}</li>"; } } }
运行实例 »
点击 "运行实例" 按钮查看在线实例