【thinkphp3.x】thinkphp3.x中连接mysql数据库

 

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

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

示例:

<?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文件,内容如下:

<?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,数据库中就会有新的记录生成;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值