tp数据库操作

tp数据库

连接

改配置文件

改applacation/database文件

 // 数据库类型
    'type'            => 'mysql',
    // 服务器地址
    'hostname'        => '127.0.0.1',
    // 数据库名
    'database'        => 'tp_com',
    // 用户名
    'username'        => 'test',
    // 密码
    'password'        => '123456',
    // 端口
    'hostport'        => '3306',

使用方法配置

  • 使用数组
Db::connect([
// 数据库类型
'type' => 'mysql',
// 数据库连接DSN配置
'dsn' => '',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'thinkphp',
// 数据库用户名
'username' => 'root',
// 数据库密码
'password' => '',
// 数据库连接端口
'hostport' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'think_',
]);

  • 使用字符串

    Db::connect('mysql://root:1234@127.0.0.1:3306/thinkphp#utf8');
    //数据库类型://用户名:密码@数据库地址:数据库端口/数据库名#字符集
    
    

用模型类连接

见下文检测连接成功中第三点

用查询检测是否连接成功

1.tp方法

<?php
namespace app\index\controller;
use think\Db;
class Index
{
    public function data()
    {
        //实例化数据库类
        $Db=new Db();
        //查询数据
        $data=$Db::table("user")->select();
        dump($data);
    }
}

2.使用sql语句

<?php
namespace app\index\controller;
use think\Db;
class Index
{
    public function data()
    {
        //实例化数据库类
        $Db=new Db();
        //查询数据
        $data=$Db::query("select * from user");
        dump($data);
    }
}

3.模型类定义

  • 创建数据模型

    命令行打开tp5所在目录,使用命令行

    php think make:model app\index\model\User
    

    直接在application/index新建文件夹model

  • 连接数据库

    model/user.php

    use think\Model;
    
    class User extends Model
    {
        //使用数据库配置连接数据库
        protected $connection = [
    // 数据库类型
            'type' => 'mysql',
            // 数据库连接DSN配置
            'dsn' => '',
    // 服务器地址
            'hostname' => '127.0.0.1',
    // 数据库名
            'database' => 'thinkphp',
    // 数据库用户名
            'username' => 'root',
    // 数据库密码
            'password' => '',
    // 数据库连接端口
            'hostport' => '',
    // 数据库连接参数
            'params' => [],
    // 数据库编码默认采用utf8
            'charset' => 'utf8',
    // 数据库表前缀
            'prefix' => 'think_',
        ];
    
    }
    
  • 控制器中使用

    //实例化模型
    $user=new user();
    //查询所有数据
    dump($user::all);
    

基本使用

  • 自定义控制器
<?php
//声明命名空间
namespace app\index\controller;
//导入系统控制器类
use think\Controller;
use think\Db;

class User extends Controller
{
    public function index()
    {
        return "user-index";
    }

    //增
    public function add()
    {
        $data= Db::execute("insert into  user value (111,'Mary','123456')");
        //返回值是影响行数
        dump($data);
    }
    //删
    public function del()
    {
        // 根据主键删除
        Db::table('user')->delete(1);
        Db::execute("delete from user where id=1");
        Db::table('user')->delete([1,2,3]);
     //条件删除
        Db::table('user')->where('id',1)->delete();
        Db::table('user')->where('id','<',10)->delete();

    }
    //改
    public function update()
    {
        Db::execute('update user set id=10 where id=2');
    }

    //查
    public function select()
    {
        $data= Db::query("select * from user");
        dump($data);
    }
}
  • 使用资源控制器

    cmd:E:\phpstudy_pro\WWW\tp5>php think make:controller app\index\controller\Users

    写资源路由

    <?php
    use think\Route;
    
    Route::resource('user','index/users');
    

    修改生成的控制器文件

    controller.php

    <?php
    
    namespace app\index\controller;
    
    use think\Controller;
    use think\Request;
    use think\Db;
    
    class Users extends Controller
    {
        /**
         * 显示资源列表
         *
         * @return \think\Response
         */
        public function index()
        {
            //查询资源列表
            $data=Db::query("select * from users");
            //dump($data);
    
            //给页面分配数据
            $this->assign("data",$data);
    
            //加载页面
            return view();
        }
    
        /**
         * 显示创建资源表单页.
         *
         * @return \think\Response
         */
        public function create()
        {
            //查询资源列表
            $data=Db::query("select * from users");
            //dump($data);
    
            //给页面分配数据
            $this->assign("data",$data);
            return view();
        }
    
        /**
         * 保存新建的资源
         *
         * @param  \think\Request  $request
         * @return \think\Response
         */
        public function save(Request $request)
        {
            //处理数据
            $data=input("post.");
            dump($data);
    
            //执行数据库插入
            //$code=Db::execute("insert into users value(null,':name',':password',':age'),$data");
            $code=Db::table('users')->insert($data);
            if($code){
                $this->success("添加成功");
            }else{
                $this->error("添加失败");
            }
        }
    
        /**
         * 显示指定的资源
         *
         * @param  int  $id
         * @return \think\Response
         */
        public function read($id)
        {
            //
        }
    
        /**
         * 显示编辑资源表单页.
         *
         * @param  int  $id
         * @return \think\Response
         */
        public function edit($id)
        {
            //从数据库中查询被修改的数据
            $data=Db::table('users')->where('id',$id)->find();
            dump($data);
            //分配数据
            $this->assign("data",$data);
            //加载页面
            return view();
    
        }
    
        /**
         * 保存更新的资源
         *
         * @param  \think\Request  $request
         * @param  int  $id
         * @return \think\Response
         */
        public function update(Request $request, $id)
        {
            //接收数据
            $data=Request::instance()->except('_method');
            //dump($data);
            $code=Db::table('users')->where('id',$id)->update([
                'name'=>$data['name'],'password'=>$data['password'],'age'=>$data['age'],
            ]);
    
            if($code){
                $this->success("数据更新成功");
            }else {
                $this->error("更新失败");
            }
        }
    
        /**
         * 删除指定资源
         *
         * @param  int  $id
         * @return \think\Response
         */
        public function delete($id)
        {
            //
            $code=Db::table('users')->delete($id);
           if($code)
           {
               $this->success("删除成功");
           }else{
               $this->error("删除失败");
           }
        }
    }
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值