• 技术文章 >后端开发 >php教程

    ThinkPHP框架PDO连接数据库步骤详解

    php中世界最好的语言php中世界最好的语言2018-05-17 14:48:07原创1262

    这次给大家带来ThinkPHP框架PDO连接数据库步骤详解,ThinkPHP框架PDO连接数据库的注意事项有哪些,下面就是实战案例,一起来看一下。

    本文实例讲述了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>

    二 运行结果

    相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

    推荐阅读:

    PHP计算个人所得税步骤详解(附代码)

    thinkPHP控制器变量在模板内显示步骤详解

    以上就是ThinkPHP框架PDO连接数据库步骤详解的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:ThinkPHP 数据库 php
    上一篇:关于PHP的命名空间(结合代码实例,简单粗暴易懂) 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 聊聊PHP escapeshellarg函数使用的中文问题• PHP原生类的总结分享• 分享PHP函数使用小工具(附代码示例)• PHP安全编码总结(经验分享)• 非常全面!PHP常见漏洞代码总结!
    1/1

    PHP中文网