本篇文章给大家分享的是有关使用ThinkPHP怎么连接数据库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一 代码
1、完成入口函数的编写
2、完成控制器的编写
getInstance($db_dsn); //连接数据库,返回数据库驱动类
$select=$conn->query('select * from think_user'); //执行查询语句
$this->assign('select',$select); // 模板变量赋值
$this->display(); // 指定模板页
}
public function type(){
$dsn = array(
'dbms' => 'mysql',
'username' => 'root',
'password' => 'root',
'hostname' => 'localhost',
'hostport' => '3306',
'database' => 'db_database30'
);
$db = new Db();
$conn=$db->getInstance($dsn); //连接数据库,返回数据库驱动类
$select=$conn->query('select * from think_type'); //执行查询语句
$this->assign('select',$select); // 模板变量赋值
$this->display('type'); // 指定模板页
}
}
?>3、完成模板编写
用户信息输出
| 用户信息 | ||
| ID | 名称 | 地址 |
| {$user.id} | {$user.user} | {$user.address} |
类别输出
| 类别输出 | ||
| ID | 类别名称 | 添加时间 |
| {$type.id} | {$type.typename} | {$type.dates} |
二 运行结果


以上就是使用ThinkPHP怎么连接数据库,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。