本文執行個體講述了thinkphp3.x串連mysql資料庫的方法。分享給大家供大家參考,具體如下:
慣例設定檔:ThinkPHP/conf/convention.php
(1)在設定檔中填寫配置資訊(設定檔:“./xmall/conf/config.php”):
樣本:
<?phpreturn 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檔案,內容如下:
<?phpclass 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程式設計有所協助。