一、设置mysql数据库的参数

thinkphp\Application\Home\Conf\config.php

<?php
return array(
//'配置项'=>'配置值'
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'mydb', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '123', // 密码
'DB_PORT' => '3306', // 端口
'DB_PREFIX' => '', // 数据库表前缀
'DB_PARAMS' => array(), // 数据库连接参数
'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
'DB_FIELDS_CACHE' => true, // 启用字段缓存
'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8
'DB_DEPLOY_TYPE' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'DB_RW_SEPARATE' => false, // 数据库读写是否分离 主从式有效
'DB_MASTER_NUM' => 1, // 读写分离后 主服务器数量
'DB_SLAVE_NO' => '' // 指定从服务器序号
);

二、编写连接数据库的代码

本示例是查询city表的第一行记录的cityname字段,然后将cityname字段的内容显示在页面上

thinkphp\Application\Home\Controller\Demo1Controller.class.php

<?php
namespace Home\Controller;
use Think\Controller; class Demo1Controller extends Controller {
public function index(){
$city = M("city")->select();
$this->assign('cityname',$city[0]['cityname']);
$this->display();
}
}

thinkphp\Application\Home\View\Demo1\index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello,{$name}!
</body>
</html>

三、查询一个表,并且显示表中的数据

thinkphp\Application\Home\Controller\Demo1Controller.class.php

<?php
namespace Home\Controller;
use Think\Controller; class Demo1Controller extends Controller {
public function index(){
$user = M("city")->select();
$this->assign('list',$user);
$this->display();
}
}

thinkphp\Application\Home\View\Demo1\index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo1</title>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>序号</td>
<td>城市</td>
<td>省会</td>
<td>描述</td>
</tr>
<foreach name="list" item="item" key="index">
<tr>
<td>{$index+1}</td>
<td>{$item.cityname}</td>
<td>{$item.province}</td>
<td>{$item.citydesc}</td>
</tr>
</foreach>
</table>
</body>
</html>

foreach是thinkphp内置的标签

四、将从数据库中查询中的数据以json的格式返回

<?php
namespace Home\Controller;
use Think\Controller; class Demo1Controller extends Controller { public function data(){
$subject = M("tbsubject")->field('id,subjectname')->select(); $this->ajaxReturn($subject,'JSON');
}
}

最新文章

  1. Three.Js学习第一天
  2. 【Swift学习】Swift编程之旅---函数(十)
  3. HTML &lt;fieldset&gt; 标签将表单内的相关元素分组
  4. HTML之表单元素
  5. Java Bytecode Instructions List
  6. C# WinForm动态添加MSChart控件
  7. web.xml中contextConfigLocation的作用
  8. 【BZOJ】【3669】【NOI2014】魔法森林
  9. 图源点到各个点的最短路径(DIJ)
  10. 24种设计模式--适配器模式【Adapter Pattern】
  11. 如何在AD6(altium designer 6.x)中设置PCB大小
  12. UILabel Text 加下划线
  13. 异常:cvc-complex-type.2.4.a: Invalid content was found starting with element
  14. 十一:Java之GUI图形Awt和Swing
  15. Lamada转化字符类型
  16. 1013团队Beta冲刺day4
  17. pytorch识别CIFAR10:训练ResNet-34(准确率80%)
  18. centos系统安装第三方源EPEL
  19. 这就涉及到ABAQUS历史输出中各能量变量的意义
  20. centos 7 mariadb安装

热门文章

  1. TCP/IP——IP网络协议简记
  2. openvpn部署centos7
  3. PHP 笔记——Web页面交互
  4. Tsinsen Palisection
  5. 【BZOJ】2131: 免费的馅饼
  6. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
  7. 《深入理解Spark-核心思想与源码分析》(五)第五章任务提交与执行
  8. 提示框框架KVNProgress介绍
  9. MYSQL学习笔记 (三)JOIN用法
  10. Object-C编程基础总结: