这次给大家带来ThinkPHP连接数据库操作案列分析,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} |
二 运行结果


相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
PHP move_uploaded_file() 函数实战案例详解











