Blogger Information
Blog 14
fans 0
comment 0
visits 16079
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
创建数据库连接 静态/动态
依然很m丶的博客
Original
1026 people have browsed it

静态连接:

<?php
namespace app\index\controller;
use \think\Db;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //1.获取数据库的连接实例/对象
        $link = Db::connect();
        //2.用连接实例调用查询类的查询方法
        $result = $link->table('shop_name')->select();
        //输出查询结果
        dump($result);
    }
}

动态连接:

<?php
namespace app\index\controller;
use \think\Db;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        $config = [
            'type'=>'mysql',
            'hostname'=>'localhost',
            'username'=>'root',
            'password'=>'123',
            'database'=>'shop',
        ];
        // $config = 'mysql://root:123@localhost:3306/shop#utf8'; //动态配置连接字符串
        //1.获取数据库的连接实例/对象
        $link = Db::connect($config);
        //2.用连接实例调用查询类的查询方法
        $result = $link->table('shop_name')->select();
        //输出查询结果
        dump($result);
    }
}

或者直接使用 Db类:

dump( Db::table('shop_name')->select());





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