本文實(shí)例講述了thinkphp3.x連接mysql數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
慣例配置文件:ThinkPHP/conf/convention.php
(1)在配置文件中填寫配置信息(配置文件:“./xmall/conf/config.php”):
示例:
<?php return array( //'配置項(xiàng)'=>'配置值' /* 數(shù)據(jù)庫設(shè)置 */ 'DB_TYPE' => 'mysql', // 數(shù)據(jù)庫類型 'DB_HOST' => 'localhost', // 服務(wù)器地址 'DB_NAME' => 'xmall', // 數(shù)據(jù)庫名 'DB_USER' => 'root', // 用戶名 'DB_PWD' => '123', // 密碼 'DB_PORT' => '3306', // 端口 'DB_PREFIX' => 'think_', // 數(shù)據(jù)庫表前綴 'DB_FIELDTYPE_CHECK' => false, // 是否進(jìn)行字段類型檢查 'DB_FIELDS_CACHE' => true, // 啟用字段緩存 'DB_CHARSET' => 'utf8', // 數(shù)據(jù)庫編碼默認(rèn)采用utf8 ); ?>
(2)創(chuàng)建表:
CREATE TABLE `think_user` ( `id` int(11) DEFAULT NULL, `name` varchar(30) DEFAULT NULL, `pwd` varchar(20) DEFAULT NULL ) ENGINE=InnoDB;
(3) 執(zhí)行數(shù)據(jù)插入操作在lib/Action下修改IndexAction.class.php文件,內(nèi)容如下:
<?php class IndexAction extends Action{ function index(){ public function index(){ $data=array( "id"=>"1", "name="=>"liuning", "pwd"=>"asd123" ); M("user")->add($data); } } } ?>
(4)執(zhí)行http://localhost/xmall/index.php,數(shù)據(jù)庫中就會(huì)有新的記錄生成;
PS:這里推薦幾款本站的格式化美化工具,相信大家在以后的開發(fā)中能夠用得上:
php代碼在線格式化美化工具:
http://tools.gimoo.net/code/phpformat
JavaScript代碼美化/壓縮/格式化/加密工具:
http://tools.gimoo.net/code/jscompress
在線XML格式化/壓縮工具:
http://tools.gimoo.net/code/xmlformat
JSON代碼格式化美化工具:
http://tools.gimoo.net/code/json
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.gimoo.net/code/xmljson
sql代碼在線格式化美化工具:
http://tools.gimoo.net/code/sqlcodeformat
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。