当前位置:Gxlcms > php框架 > thinkphp3.x连接mysql数据库的方法(具体操作步骤)

thinkphp3.x连接mysql数据库的方法(具体操作步骤)

时间:2021-07-01 10:21:17 帮助过:7人阅读

本文实例讲述了thinkphp3.x连接mysql数据库的方法。分享给大家供大家参考,具体如下:

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

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

示例:

  1. <?php
  2. return array(
  3. //'配置项'=>'配置值'
  4. /* 数据库设置 */
  5. 'DB_TYPE' => 'mysql', // 数据库类型
  6. 'DB_HOST' => 'localhost', // 服务器地址
  7. 'DB_NAME' => 'xmall', // 数据库名
  8. 'DB_USER' => 'root', // 用户名
  9. 'DB_PWD' => '123', // 密码
  10. 'DB_PORT' => '3306', // 端口
  11. 'DB_PREFIX' => 'think_', // 数据库表前缀
  12. 'DB_FIELDTYPE_CHECK' => false, // 是否进行字段类型检查
  13. 'DB_FIELDS_CACHE' => true, // 启用字段缓存
  14. 'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8
  15. );
  16. ?>

(2)创建表:

  1. CREATE TABLE `think_user` (
  2. `id` int(11) DEFAULT NULL,
  3. `name` varchar(30) DEFAULT NULL,
  4. `pwd` varchar(20) DEFAULT NULL
  5. ) ENGINE=InnoDB;

(3) 执行数据插入操作在lib/Action下修改IndexAction.class.php文件,内容如下:

  1. <?php
  2. class IndexAction extends Action{
  3. function index(){
  4. public function index(){
  5. $data=array(
  6. "id"=>"1",
  7. "name="=>"liuning",
  8. "pwd"=>"asd123"
  9. );
  10. M("user")->add($data);
  11. }
  12. }
  13. }
  14. ?>

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

PS:这里推荐几款本站的格式化美化工具,相信大家在以后的开发中能够用得上:

php代码在线格式化美化工具:
http://tools.jb51.net/code/phpformat

JavaScript代码美化/压缩/格式化/加密工具:
http://tools.jb51.net/code/jscompress

在线XML格式化/压缩工具:
http://tools.jb51.net/code/xmlformat

JSON代码格式化美化工具:
http://tools.jb51.net/code/json

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

sql代码在线格式化美化工具:
http://tools.jb51.net/code/sqlcodeformat

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

人气教程排行