本文實例講述了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,數據庫中就會有新的記錄生成;