这篇文章将为大家详细讲解有关使用thinkPHP5框架如何连接数据库,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1、配置文件目录 tp5\application\database.php
通过配置文件来连接。。
在控制器里方法链接数据库 ;查询时写法 和使用系统的DB类方法略有差异
// 使用方法配置数据库连接
public function data1 ()
{
$DB = Db::connect([
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'user',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
// 端口
'hostport' => '3306',
]);
// dump($DB);
// 查询数据,,,,和使用系统的DB类方法略有差异
$data = $DB -> table("uu") -> select();
dump($data);
}2.基本使用 、 增删改查
控制器使用配置文件连接数据库
控制器下文件(tp5\application\index\controller\Index.php)写入
fetch();
}
// 使用配置文件连接数据库
public function data()
{
// 实例化数据库系统类
$DB = new Db;
// 查询数据,表名为uu的所有数据
$data = $DB::table("uu") -> select();
// 使用sql语句
//$data = $DB::query("select * from uu");
dump($data);
}
}http://yourwebname/public/index.php/index/Index/data 获取数据打印测试
3.将数据渲染模板页面
data();
return $this -> fetch();
}
// 使用系统配置文件连接数据库
public function data()
{
// 实例化数据库系统类
$DB = new Db;
// 查询数据
$data = $DB::table("uu") -> select();
$this -> assign("user",$data);
// dump($data);
}
}4.模板页面即可引用渲染数据
tp5\application\index\view\index\index.html
s s{volist name="user" id="vo"} {$vo.name} {/volist}
关于使用thinkPHP5框架如何连接数据库就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。