一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Thinkphp连接数据库的学习笔记

时间:2016-03-22 编辑:简简单单 来源:一聚教程网

学习到 thinkphp 建立一个小实例,就从网上找了一篇文章,本身 thinkphp 是有官网实例的,但老是出错,下面这篇文章还不错,算是最基础的了,从配置文件到连接数据库,然后再到制作模板,读取模板,还有新建 mysql 数据库,都比较详细,thinkphp 底层核心文件不变,如果刚学习可以参照着学习一下。
测试所在系统:WIN7、XP
测试工具:zend studio9
运行平台:WAMP
数据库:mysql
TP测试版本:3.0(强调:3.0之前版本在配置上有很大不同)
1. 配置入口文件
入口文件:与ThinkPHP在同一级别目录下
命名:index.php
index.php
define( 'THINK_PATH', './ThinkPHP/' ); 
define( 'APP_PATH', './web3.0/' ); 
define( 'APP_NAME', 'web3.0' ); 
require THINK_PATH.'ThinkPHP.php'; 
?> 
运行结果:看到欢迎界面^_^ Hello,欢迎使用ThinkPHP既配置入口成功。
2. 配置数据库链接方式
文件路径:盘符:\服务器路径\TP3.0WEb\web3.0\Conf
打开config.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.0WEb\web3.0\Lib\Action\IndexAction.class.php
打开IndexAction.class.php文件,并修改成如下内容:
// 本类由系统自动生成,仅供测试用途 
class IndexAction extends Action { 
 public function index() 
 { 
  $form = D( 'Form' )->findall();    // 推荐不要使用封装好的数据库查询方法,细节日后更新 
  dump( $form ); 
  //$this->display(); 
 } 

?> 
4.配置数据库查询语句文件
文件路径:盘符:\服务器路径\TP3.0WEb\web3.0\Lib\Model
在该文件下建立一个模板文件(不知道这么称对不对)FormModel.class.php
->盘符:\服务器路径\TP3.0WEb\web3.0\Lib\Model\FormModel.class.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.0WEb\web3.0\Tpl
在Tpl目录下建立Index(I是大写)文件夹,然后再该文件夹下再建一个index.html(i是小写)文件
->盘符:\服务器路径\TP3.0WEb\web3.0\Tpl\Index\index.html
强调:在2.0中需要在Tpl目录下建立Default文件夹然后再建立Index文件夹,并在Index文件夹下再建立index.html才算配置完毕
在index.html中添加以下内容:
 
 
  
 {$title} 
 
 
 
 {$title} 
 
 
运行后可以看见{$title}的内容被替换了

6.修改步骤4的内容,路径不变。

文件路径:盘符:\服务器路径\TP3.0WEb\web3.0\Lib\Model
将FormModel.class.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,然后通过对应的名称和路径实现模板替换 
 } 

?> 

连接数据库用法示例

惯例配置文件:ThinkPHP/conf/convention.php

(1)在配置文件中填写配置信息(配置文件:“./xmall/conf/config.php”):

示例:

return array(
 //'配置项'=>'配置值'
 /* 数据库设置 */
'DB_TYPE'       => 'mysql',     // 数据库类型
'DB_HOST'       => 'localhost', // 服务器地址
'DB_NAME'       => 'xmall',      // 数据库名
'DB_USER'       => 'root',      // 用户名
'DB_PWD'=> '123',    // 密码
'DB_PORT'       => '3306',      // 端口
'DB_PREFIX'     => 'think_',    // 数据库表前缀   
'DB_FIELDTYPE_CHECK'    => false,       // 是否进行字段类型检查   
'DB_FIELDS_CACHE'       => true,// 启用字段缓存   
'DB_CHARSET'    => 'utf8',      // 数据库编码默认采用utf8
);
?>

(2)创建表:

CREATE TABLE `think_user` (
    `id` int(11) DEFAULT NULL,
    `name` varchar(30) DEFAULT NULL,
    `pwd` varchar(20) DEFAULT NULL
) ENGINE=InnoDB;
 
(3) 执行数据插入操作在lib/Action下修改IndexAction.class.php文件,内容如下:

class IndexAction extends Action{
 function index(){
   public function index(){
    $data=array(
     "id"=>"1",
     "name="=>"liuning",
     "pwd"=>"asd123"
    );
    M("user")->add($data);
  }
 }
}
?>

(4)执行http://localhost/xmall/index.php,数据库中就会有新的记录生成;

热门栏目