本文实例讲述了thinkphp框架基于pdo方式连接数据库操作。分享给大家供大家参考,具体如下:
一 代码
1、修改config.php文件
<?php return array( 'db_type'=> 'pdo', // 注意dsn的配置针对不同的数据库有所区别 'db_dsn'=> 'mysql:host=localhost;dbname=db_database30', 'db_user'=>'root', 'db_pwd'=>'root', 'db_prefix'=>'think_', // 其他项目配置参数……… 'app_debug' => true, // 关闭调试模式 'show_page_trace'=>true, ); ?>
2、创建控制器
<?php
header("content-type:text/html; charset=utf-8"); //设置页面编码格式
class indexaction extends action{
public function index(){
$db = m('user'); // 实例化模型类,参数数据表名称,不包含前缀
$select = $db->select(); // 查询数据
$this->assign('select',$select); // 模板变量赋值
$this->display(); // 指定模板页
}
public function type(){
$dba = m('type'); // 实例化模型类,参数数据表名称,不包含前缀
$select = $dba->select(); // 查询数据
$this->assign('select',$select); // 模板变量赋值
$this->display('type'); // 指定模板页
}
}
?>
3、创建入口文件
<?php
define('think_path', '../thinkphp'); //定义thinkphp框架路径(相对于入口文件)
define('app_name', 'app'); //定义项目名称
define('app_path', './app'); //定义项目路径
require(think_path."/thinkphp.php"); //加载框架入口文件
app::run(); //实例化一个网站应用实例
?>
4、创建模板文件
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>用户信息输出</title>
<link href="__root__/public/css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99cc33" bordercolor="#ffffff">
<tr>
<td colspan="3" bgcolor="#ffffff" class="title" align="center">用户信息</td>
</tr>
<tr class="title">
<td bgcolor="#ffffff" width="44">id</td>
<td bgcolor="#ffffff" width="120">名称</td>
<td bgcolor="#ffffff" width="223">地址</td>
</tr>
<volist name='select' id='user' >
<tr class="content">
<td bgcolor="#ffffff"> {$user.id}</td>
<td bgcolor="#ffffff"> {$user.user}</td>
<td bgcolor="#ffffff"> {$user.address}</td>
</tr>
</volist>
</table>
</body>
</html>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>类别输出</title>
<link href="__root__/public/css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99cc33" bordercolor="#ffffff">
<tr>
<td colspan="3" bgcolor="#ffffff" class="title" align="center">类别输出</td>
</tr>
<tr class="title">
<td bgcolor="#ffffff" width="44">id</td>
<td bgcolor="#ffffff" width="120">类别名称</td>
<td bgcolor="#ffffff" width="223">添加时间</td>
</tr>
<volist name='select' id='type' >
<tr class="content">
<td bgcolor="#ffffff"> {$type.id}</td>
<td bgcolor="#ffffff"> {$type.typename}</td>
<td bgcolor="#ffffff"> {$type.dates}</td>
</tr>
</volist>
</table>
</body>
</html>
二 运行结果
更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《zend framework框架入门教程》及《php模板技术总结》。
希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!