Blogger Information
Blog 46
fans 2
comment 0
visits 19142
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1、下载thinkPHP 2、搭建thinkphp项目 3、配置 /config/app.php文件, /config/database.php文件 4、数据库操作:增删查改
P粉314265155
Original
514 people have browsed it

下载thinkPHP 搭建thinkphp项目

欧阳克博客
http://www.ouyangke.com/back/thinkphp/1%E5%AE%89%E8%A3%85.html

配置 /config/app.php文件

`
<?php
// +———————————————————————————————————
// | 应用设置
// +———————————————————————————————————

return [
// 应用地址
‘app_host’ => env(‘app.host’, ‘’),
// 应用的命名空间
‘app_namespace’ => ‘’,
// 是否启用路由
‘with_route’ => true,
// 默认应用
‘default_app’ => ‘index’,
// 默认时区
‘default_timezone’ => ‘Asia/Shanghai’,

  1. // 应用映射(自动多应用模式有效)
  2. 'app_map' => [],
  3. // 域名绑定(自动多应用模式有效)
  4. 'domain_bind' => [],
  5. // 禁止URL访问的应用列表(自动多应用模式有效)
  6. 'deny_app_list' => [],
  7. // 异常页面的模板文件 报错误页面
  8. 'exception_tmpl' => app()->getThinkPath() . 'tpl/think_exception.tpl',
  9. // 错误显示信息,非调试模式有效
  10. 'error_message' => '我是config/app.php页面错误提示!请稍后再试~',
  11. // 显示错误信息 开发调试 true 上线 为 false
  12. 'show_error_msg' => true,

];

`

数据库操作:增删查改

<?php
namespace app\controller;

use app\BaseController;
// 引入数据库门面类
use think\facade\Db;

// 每个文件必有一个class 不会有多个class
// 类名要与文件名保持一致
class Index extends BaseController
{
// 所有代码都在方法里面写
public function index()
{
// return ‘<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: “Century Gothic”,”Microsoft yahei”; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V’ . \think\facade\App::version() . ‘<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>‘;
// return ‘123456789’;
// 数据库操作统一入口 使用链式操作
// 2.1必须使用Db:: 来使用 数据库操作
// 2.2查询表明 使用 table方法
// 2.3 操作数据库 需要多个功能 需要使用链式操作
// 对象 tp 把数据多封装了一层。方便处理
// $ret= Db:: table(‘lh_user’) -> select( );
// print_r($ret);
// print_r($ret[0][‘name’]);

  1. // 2.4 把 tp返回的数据类型 改为数组
  2. // $arr = $ret -> toArray();
  3. // print_r($arr);

// 2.5 mysql 语句增加条件 条件参数可以多种参数
// $ret = Db::table(‘lh_user’)->where(‘uid’, 1)-> select( );
// $ret = Db::table(‘lh_user’)->where(‘uid’,’<’ ,4)-> select( );
// 一维数组 查询单条数据 使用 find()方法
// $ret = Db::table(‘lh_user’)->where(‘uid’, 1)->find();

  1. // $ret = Db::table('lh_user')->where('uid', '<',4)->find();
  2. // $ret = Db::table('lh_user')->where('uid', 1)->select()->toArray();
  3. // $ret = Db::table('lh_user')->where('uid', 1)->value('name');
  4. // 将查询结果的主键 值正确不显示 不再从 0 开始
  5. // $ret = Db::table('lh_user')->where('uid',1)->column('name','uid');
  6. // $ret = Db::table('lh_user')->where('uid',2)->column('name');
  7. // print_r($ret);

// 2.6mysql 语句查询单条数据,可以使用find方法
// 外部传入的值
// $uid = $_GET[‘uid’];
// $ret = Db::table(‘lh_user’)->where(‘uid’, $uid)->find();
// print_r($ret);
// 2.7 field 返回的字段名,返回的是数组
// $ret = Db::table(‘lh_user’) ->field(‘name’) -> where(‘uid’,2) -> find();
// 返回数组
// print_r($ret);
// 返回 值 非数组
// print_r($ret[‘name’]);
// 2.8 value 返回字段的值
// $ret = Db::table(‘lh_user’) -> where(‘uid’,2) -> value(‘name’);
// 返回 值 非数组
// print_r($ret);
// 2.9 返回一列的值
// $ret = Db::table(‘lh_user’) -> column(‘name’) ;
// 第二个 值为 下标的值
// $ret = Db::table(‘lh_user’) -> column(‘name’ , ‘uid’) ;
// $ret = Db::table(‘lh_user’) -> column(‘name’ , ‘phone’) ;
// print_r($ret);
// 3 添加数据 insert 使用数组的方式, key = value 操作
// md5 方法 是php 自带的 把字符串进行md5 加密 无法解密 非常安全保障安全
// $data = [
// ‘name’ => ‘小孔’,
// ‘account’ => ‘xiaokong’,
// ‘phone’ => ‘18963300001’,
// ‘password’ => 123456,
// ‘add_time’ => time(),
// ‘last_time’ => time(),
// ];
// $ret = Db::table(‘lh_user’) -> insert($data );
// 打印值 是 1表示成功
// print_r($ret);

// 4、 update 修改数据 库的数据 必须增加条件,不然所以数据都会修改只能跑路
// $data =[
// ‘password’ => 123 ,
// ‘last_time’ => time(),
// ];

// $ret = Db::table(‘lh_user’) -> where(‘uid’,1) -> update($data);
// print_r($ret);

// 5、删除数 据 在程序员中 是不允许删除的,程序员不做删除的功能
// 可以使用 更改状态,或者使用其他功能 字段进行代替删除
// delete() 方法
// $ret = Db::table(‘lh_user’) ->where(‘uid’,12) ->delete();
// print_r($ret);

// 6、 query()方法 直接写 mysql语句即可 原生的 双引号注意
// 全部查询
// $ret = Db::query(“SELECT * FROM lh_user WHERE uid > 4 AND uid <7"); // 部分查询 // $ret = Db::query("SELECT `name`,`phone` FROM `lh_user` WHERE `uid` > 4 AND uid <7”);
// print_r($ret);
// 7、 execute()方法 直接写 mysql语句即可 原生的 双引号注意
// 插入数据 原生的mysql操作
// $ret = Db::execute(“INSERT INTO lh_user VALUE (null ,’xiaobian’,456,’小编’,’18963301234’ ,1,1659283200,1659283200) “);
// print_r($ret);

// 8、 添加多条数据 insertALL 重要数据单条处理
// 多条数据是二维数组

  1. // $data =[
  2. // [
  3. // 'name' => '小孔',
  4. // 'account' => 'xiaokong',
  5. // 'phone' => '18963300001',
  6. // 'password' => 123456,
  7. // 'add_time' => time(),
  8. // 'last_time' => time(),
  9. // ],
  10. // [
  11. // 'name' => '小黑',
  12. // 'account' => 'xiaokong',
  13. // 'phone' => '18963300001',
  14. // 'password' => 123456,
  15. // 'add_time' => time(),
  16. // 'last_time' => time(),
  17. // ],
  18. // ];
  19. // $ret = Db::table('lh_user') -> insertALL($data);
  20. // 插入一条数据 显示 1插入两条显示 2插入三条显示 3
  21. // print_r($ret);

// 9、 insertGetId () 添加数据成功后,返回自增的 id 可以查询最后一条成功的数据
// $data = [
// ‘name’ => ‘小孔’,
// ‘account’ => ‘xiaokong’,
// ‘phone’ => ‘18963300001’,
// ‘password’ => 123456,
// ‘add_time’ => time(),
// ‘last_time’ => time(),
// ];
// $ret = Db::table(‘lh_user’) -> insertGetId($data );
// 添加成功 是 成功的自增id值 失败 是 0 可进行判断
// print_r($ret);
// 添加成功 是 成功的自增id值 失败 是 0 可进行判断
// if(empty($ret)){
// echo ‘失败’;
// }else{
// echo ‘成功’;
// }

// 10、 useSoftDelete(‘status’,2) 软删除 数据 使用字段代替 但是数据存在在 数据表中
// $ret =Db::table(‘lh_user’) -> where(‘uid’,18)-> useSoftDelete(‘status’,2)->delete();
// print_r($ret);

  1. }
  2. public function hello($name = 'ThinkPHP6')
  3. {
  4. return 'hello,' . $name;
  5. }
  6. public function php(){
  7. return 'php';
  8. }

}

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!