thinkphp3.x串連mysql資料庫的方法(具體操作步驟)_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.