zoukankan      html  css  js  c++  java
  • 最详细、最简单Thinkphp链接数据库

    测试所在系统:WIN7、XP
    测试工具:zend studio9
    运行平台:WAMP
    数据库:mysql
    TP测试版本:3.0(强调:3.0之前版本在配置上有很大不同)

    1. 配置入口文件
    入口文件:与ThinkPHP在同一级别目录下
    命名:index.php

    index.php

     <?php
     define( 'THINK_PATH', './ThinkPHP/' );

     define( 'APP_PATH', './web3.0/' );
     define( 'APP_NAME', 'web3.0' );

     require THINK_PATH.'ThinkPHP.php';
     ?>

    运行结果:看到欢迎界面^_^ Hello,欢迎使用ThinkPHP既配置入口成功。

    2. 配置数据库链接方式
    文件路径:盘符:服务器路径TP3.0WEbweb3.0Conf
    打开config.php文件并修改成以下内容:

     <?php
     return array(
      //'配置项'=>'配置值'
      'DB_TYPE'=>'mysql',
      'DB_HOST'=>'localhost',
      'DB_NAME'=>'myapp',     // 数据库名为myapp
      'DB_USER'=>'root',
      'DB_PWD'=>'',
      'DB_PORT'=>'3306',
      'DB_PREFIX'=>'think_',
      // 由于最简单的链接方式,故缩略些功能
     );
     ?>

     数据库名称是myapp,mysql操作方式如下:

     <1>.建数据库
      CREATE DATABASE `myapp` ;
      
     <2>.建数据表(think_form既是数据表的名称)

     CREATE TABLE `think_form` (
     `id` smallint(4) unsigned NOT NULL auto_increment,
     `title` varchar(255) NOT NULL,
     `content` varchar(255) NOT NULL,
     `create_time` int(11) unsigned NOT NULL,
     `update_time` int(11) unsigned NOT NULL,
     `status` tinyint(1) unsigned NOT NULL,
     `email` varchar(50) NOT NULL,
     PRIMARY KEY  (`id`)
     ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

     <3>.插入数据
     INSERT INTO `think_form` (`id`, `title`, `content`, `create_time`, `update_time`, `status`, `email`) VALUES
     (1, '这是测试数据', 'dfdf', 1212724876, 0, 1, 'dddd@ddd.com')

    3. 配置模块文件
    文件路径:激活入口文件后自动生成的文件,此处文件名是web3.0
    EG:盘符:服务器路径TP3.0WEbweb3.0LibActionIndexAction.class.php

    打开IndexAction.class.php文件,并修改成如下内容

     <?php
     // 本类由系统自动生成,仅供测试用途
     class IndexAction extends Action {
      public function index()
      {
       $form = D( 'Form' )->findall();    // 推荐不要使用封装好的数据库查询方法,细节日后更新
       dump( $form );
       //$this->display();
      }
     }
     ?>

    4. 配置数据库查询语句文件
    文件路径:盘符:服务器路径TP3.0WEbweb3.0LibModel

    在该文件下建立一个模板文件(不知道这么称对不对)FormModel.class.php
     ->盘符:服务器路径TP3.0WEbweb3.0LibModelFormModel.class.php

     <?php
     class FormModel extends Model
     {
      public function findall()
      {
       $sql = 'SELECT `title` FROM think_form';
       return $this->query( $sql );
      }
     }
     ?>
     
    总结:
    创建两个文件:入口文件index.php和数据库模板文件FormModel.class.php
    修改两个文件:config.php和IndexAction.class.php

    最后 http://localhost/TP3.0WEb/index.php/index/index 查看结果。
    注明:数据库连接方法仅说明配置和实现的方法,没有说明方法的解释。详细日后更新。

    附加:实现MVC方法实现数据库数据提取并显示在html页面中。

    5. 建立数据显示模板(html中部分模板替换)
    文件路径:盘符:服务器路径TP3.0WEbweb3.0Tpl

    在Tpl目录下建立Index(I是大写)文件夹,然后再该文件夹下再建一个index.html(i是小写)文件
     ->盘符:服务器路径TP3.0WEbweb3.0TplIndexindex.html
     强调:在2.0中需要在Tpl目录下建立Default文件夹然后再建立Index文件夹,并在Index文件夹下再建立index.html才算配置完毕
     在index.html中添加以下内容:
     
     <html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>{$title}</title>
     </head>
     
     <body>
      {$title}
     </body>
     </html>
     
     运行后可以看见{$title}的内容被替换了
     
    6. 修改步骤4的内容,路径不变。
    文件路径:盘符:服务器路径TP3.0WEbweb3.0LibModel
    将FormModel.class.php模板文件中的内容改成如下:

     <?php
     // 本类由系统自动生成,仅供测试用途
     class IndexAction extends Action {
      public function index()
      {
       //$form = D( 'Form' )->findall();    上下都可以使用
       $form = M( 'Form' )->select();
       dump( $form );
       //$this->assign( 'title', $form[0]["title"] );    两种方法都一样
       $this->title = $form[0]["title"];
       $this->display();                // 要在index。html中显示内容必须打开display,然后通过对应的名称和路径实现模板替换
      }

     }
     ?>

    至此,仅有可以查询的TP数据库操作就已经完成了。
    最后 http://localhost/TP3.0WEb/index.php/index/index 查看结果。

  • 相关阅读:
    antd的form表单4.0
    antd的select搜索展现错误
    ts的枚举类型简化if else if判断
    深入解读webpack
    常用es6语法总结
    手动配置webpack
    apply,all,bind的区别
    面试题小结
    react中根据后台值动态配置
    react动态路由以及获取动态路由
  • 原文地址:https://www.cnblogs.com/xiaolongphp/p/4772696.html
Copyright © 2011-2022 走看看