ThinkPHP框架基于PDO方式连接数据库操作示例

(编辑:jimmy 日期: 2024/10/3 浏览:2)

本文实例讲述了ThinkPHP框架基于PDO方式连接数据库操作。分享给大家供大家参考,具体如下:

一 代码

1、修改config.php文件

<"htmlcode">
<"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');         // 指定模板页
  }
}
"htmlcode">
<"/ThinkPHP.php");  //加载框架入口文件
App::run();               //实例化一个网站应用实例
"htmlcode">
<!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">&nbsp;{$user.id}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$user.user}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$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">&nbsp;{$type.id}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$type.typename}</td>
  <td bgcolor="#FFFFFF">&nbsp;{$type.dates}</td>
 </tr>
 </volist>
</table>
</body>
</html>

二 运行结果

ThinkPHP框架基于PDO方式连接数据库操作示例

ThinkPHP框架基于PDO方式连接数据库操作示例

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。