thinkphp3.x連接mysql數(shù)據(jù)庫(kù)的方法(具體操作步驟)

字號(hào):


    本文實(shí)例講述了thinkphp3.x連接mysql數(shù)據(jù)庫(kù)的方法。分享給大家供大家參考,具體如下:
    慣例配置文件:ThinkPHP/conf/convention.php
    (1)在配置文件中填寫(xiě)配置信息(配置文件:“./xmall/conf/config.php”):
    示例:
    <?php
    return array(
       //'配置項(xiàng)'=>'配置值'
       /* 數(shù)據(jù)庫(kù)設(shè)置 */
        'DB_TYPE'    => 'mysql',  // 數(shù)據(jù)庫(kù)類型
        'DB_HOST'    => 'localhost', // 服務(wù)器地址
        'DB_NAME'    => 'xmall',  // 數(shù)據(jù)庫(kù)名
        'DB_USER'    => 'root',  // 用戶名
        'DB_PWD'    => '123', // 密碼
        'DB_PORT'    => '3306',  // 端口
        'DB_PREFIX'    => 'think_', // 數(shù)據(jù)庫(kù)表前綴
        'DB_FIELDTYPE_CHECK' => false,  // 是否進(jìn)行字段類型檢查
        'DB_FIELDS_CACHE'  => true,  // 啟用字段緩存
        'DB_CHARSET'   => 'utf8',  // 數(shù)據(jù)庫(kù)編碼默認(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ù)庫(kù)中就會(huì)有新的記錄生成;
    希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。