本文实例讲述了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,数据库中就会有新的记录生成;
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程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!