ThinkPHP架構PDO串連資料庫步驟詳解

來源:互聯網
上載者:User
這次給大家帶來ThinkPHP架構PDO串連資料庫步驟詳解,ThinkPHP架構PDO串連資料庫的注意事項有哪些,下面就是實戰案例,一起來看一下。

本文執行個體講述了ThinkPHP架構基於PDO方式串連資料庫操作。分享給大家供大家參考,具體如下:

一 代碼

1、修改config.php檔案

<?phpreturn array(  'DB_TYPE'=> 'pdo',  // 注意DSN的配置針對不同的資料庫有所區別  'DB_DSN'=> 'mysql:host=localhost;dbname=db_database30',  'DB_USER'=>'root',  'DB_PWD'=>'root',  'DB_PREFIX'=>'think_',  // 其他項目配置參數………  'APP_DEBUG' => true,     // 關閉偵錯模式  'SHOW_PAGE_TRACE'=>true,);?>

2、建立控制器

<?phpheader("Content-Type:text/html; charset=utf-8");  //設定頁面編碼格式class IndexAction extends Action{  public function index(){    $db = M('User');              // 執行個體化模型類,參數資料表名稱,不包含首碼    $select = $db->select();           // 查詢資料    $this->assign('select',$select);       // 模板變數賦值    $this->display();              // 指定模板頁  }  public function type(){    $dba = M('Type');              // 執行個體化模型類,參數資料表名稱,不包含首碼    $select = $dba->select();          // 查詢資料    $this->assign('select',$select);       // 模板變數賦值    $this->display('type');         // 指定模板頁  }}?>

3、建立入口檔案

<?phpdefine('THINK_PATH', '../ThinkPHP');    //定義ThinkPHP架構路徑(相對於入口檔案)define('APP_NAME', 'App');       //定義項目名稱define('APP_PATH', './App');        //定義項目路徑require(THINK_PATH."/ThinkPHP.php");  //載入架構入口檔案App::run();               //執行個體化一個網站應用程式執行個體?>

4、建立模板檔案

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>使用者資訊輸出</title><link href="ROOT/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" /></head><body><table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF"> <tr>  <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">使用者資訊</td> </tr> <tr class="title">  <td bgcolor="#FFFFFF" width="44">ID</td>  <td bgcolor="#FFFFFF" width="120">名稱</td>  <td bgcolor="#FFFFFF" width="223">地址</td> </tr> <volist name='select' id='user' > <tr class="content">  <td bgcolor="#FFFFFF"> {$user.id}</td>  <td bgcolor="#FFFFFF"> {$user.user}</td>  <td bgcolor="#FFFFFF"> {$user.address}</td> </tr> </volist></table></body></html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>類別輸出</title><link href="ROOT/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" /></head><body><table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF"> <tr>  <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">類別輸出</td> </tr> <tr class="title">  <td bgcolor="#FFFFFF" width="44">ID</td>  <td bgcolor="#FFFFFF" width="120">類別名稱</td>  <td bgcolor="#FFFFFF" width="223">添加時間</td> </tr> <volist name='select' id='type' > <tr class="content">  <td bgcolor="#FFFFFF"> {$type.id}</td>  <td bgcolor="#FFFFFF"> {$type.typename}</td>  <td bgcolor="#FFFFFF"> {$type.dates}</td> </tr> </volist></table></body></html>

二 運行結果

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

PHP計算個人所得稅步驟詳解(附代碼)

thinkPHP控制器變數在模板內顯示步驟詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.