当前位置:首页 > CMS教程 > Thinkphp > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-05 16:56:13 浏览: 评论:0 

这篇文章主要介绍了ThinkPHP框架基于PDO方式连接数据库操作,结合完整实例形式分析了thinkPHP使用PDO方式连接数据库的相关配置、控制器及模板调用相关操作技巧,需要的朋友可以参考下。

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

一 代码

1、修改config.php文件

  1. <?php 
  2. return array
  3.   'DB_TYPE'=> 'pdo'
  4.   // 注意DSN的配置针对不同的数据库有所区别 
  5.   'DB_DSN'=> 'mysql:host=localhost;dbname=db_database30'
  6.   'DB_USER'=>'root'
  7.   'DB_PWD'=>'root'
  8.   'DB_PREFIX'=>'think_'
  9.   // 其他项目配置参数……… 
  10.   'APP_DEBUG' => true,     // 关闭调试模式 
  11.   'SHOW_PAGE_TRACE'=>true, 
  12. ); 
  13. ?> 

2、创建控制器

  1. <?php 
  2. header("Content-Type:text/html; charset=utf-8");  //设置页面编码格式 
  3. class IndexAction extends Action{ 
  4.   public function index(){ 
  5.     $db = M('User');              // 实例化模型类,参数数据表名称,不包含前缀 
  6.     $select = $db->select();           // 查询数据 
  7.     $this->assign('select',$select);       // 模板变量赋值 
  8.     $this->display();              // 指定模板页 
  9.   } 
  10.   public function type(){ 
  11.     $dba = M('Type');              // 实例化模型类,参数数据表名称,不包含前缀 
  12.     $select = $dba->select();          // 查询数据 
  13.     $this->assign('select',$select);       // 模板变量赋值 
  14.     $this->display('type');         // 指定模板页 
  15.   } 
  16. ?> 

3、创建入口文件

  1. <?php 
  2. define('THINK_PATH''../ThinkPHP');    //定义ThinkPHP框架路径(相对于入口文件) 
  3. define('APP_NAME''App');       //定义项目名称 
  4. define('APP_PATH''./App');        //定义项目路径 
  5. require(THINK_PATH."/ThinkPHP.php");  //加载框架入口文件 
  6. App::run();               //实例化一个网站应用实例 
  7. ?> 

4、创建模板文件

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>用户信息输出</title> 
  6. <link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" /> 
  7. </head> 
  8. <body> 
  9. <table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF"> 
  10.  <tr> 
  11.   <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">用户信息</td> 
  12.  </tr> 
  13.  <tr class="title"> 
  14.   <td bgcolor="#FFFFFF" width="44">ID</td> 
  15.   <td bgcolor="#FFFFFF" width="120">名称</td> 
  16.   <td bgcolor="#FFFFFF" width="223">地址</td> 
  17.  </tr> 
  18.  <volist name='select' id='user' > 
  19.  <tr class="content"> 
  20.   <td bgcolor="#FFFFFF">&nbsp;{$user.id}</td> 
  21.   <td bgcolor="#FFFFFF">&nbsp;{$user.user}</td> 
  22.   <td bgcolor="#FFFFFF">&nbsp;{$user.address}</td> 
  23.  </tr> 
  24.  </volist> 
  25. </table> 
  26. </body> 
  27. </html> 
  28.  
  29. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  30. <html xmlns="http://www.w3.org/1999/xhtml"> 
  31. <head> 
  32. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  33. <title>类别输出</title> 
  34. <link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" /> 
  35. </head> 
  36. <body> 
  37. <table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF"> 
  38.  <tr> 
  39.   <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">类别输出</td> 
  40.  </tr> 
  41.  <tr class="title"> 
  42.   <td bgcolor="#FFFFFF" width="44">ID</td> 
  43.   <td bgcolor="#FFFFFF" width="120">类别名称</td> 
  44.   <td bgcolor="#FFFFFF" width="223">添加时间</td> 
  45.  </tr> 
  46.  <volist name='select' id='type' > 
  47.  <tr class="content"> 
  48.   <td bgcolor="#FFFFFF">&nbsp;{$type.id}</td> 
  49.   <td bgcolor="#FFFFFF">&nbsp;{$type.typename}</td> 
  50.   <td bgcolor="#FFFFFF">&nbsp;{$type.dates}</td> 
  51.  </tr> 
  52.  </volist> 
  53. </table> 
  54. </body> 
  55. </html>

Tags: PDO连接数据库

分享到: