• ThinkPHP:读取数据库数据 (2)


    项目配置文件Conf/config.php中添加数据库连接信息:

    // 添加数据库配置信息
    'DB_TYPE'   => 'mysql', // 数据库类型
    'DB_HOST'   => 'localhost', // 服务器地址
    'DB_NAME'   => 'thinkphp', // 数据库名
    'DB_USER'   => 'root', // 用户名
    'DB_PWD'    => '', // 密码
    'DB_PORT'   => 3306, // 端口
    'DB_PREFIX' => 'think_', // 数据库表前缀

    'DB_DSN' => 'mysql://root:密码@localhost:3306/thinkphp'

    修改下控制器

    Lib/Action/IndexAction.class.php

    class IndexAction extends Action {
        public function index(){
            $Data = M('tinyphp'); // 实例化Data数据模型,这行的tinyphp为数据表后缀名称
            $this->data = $Data->select();
            $this->display();
        }
    }

    修改模版,让数据输出

    Tpl/Index/index.html

    <html>
     <head>
       <title>Select Data</title>
     </head>
     <body>
        <volist name="data" id="vo">
        {$vo.id}--{$vo.data}<br/>
        </volist>
     </body>
    </html>

    volist标签是内置模板引擎用于输出数据集的标签。

    {$vo.id} 和 {$vo.data} 的用法和Smarty类似,就是用于输出数据的字段,这里就表示输出think_data表的id和data字段的值。

    预览效果为

    ThinkPHP3.1开发手册:http://www.thinkphp.cn/info/60.html

  • 相关阅读:
    微信发送模板消息
    主从复制 读写分离
    php nginx反向代理
    go开发工具goclipse的安装
    安装go1.11.2
    基于科大讯飞AIUI平台自定义语义库的开发
    转载--php 7.2 安装 mcrypt 扩展
    mysql取出字段数据的精度
    sublime 2 格式化json
    RESTful接口需知道
  • 原文地址:https://www.cnblogs.com/tinyphp/p/3657662.html
Copyright © 2020-2023  润新知