静态连接:
<?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());
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号