本文實(shí)例講述了ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:
一 代碼
1、修改config.php文件
?php return array( 'DB_TYPE'=> 'pdo', // 注意DSN的配置針對不同的數(shù)據(jù)庫有所區(qū)別 'DB_DSN'=> 'mysql:host=localhost;dbname=db_database30', 'DB_USER'=>'root', 'DB_PWD'=>'root', 'DB_PREFIX'=>'think_', // 其他項(xiàng)目配置參數(shù)……… 'APP_DEBUG' => true, // 關(guān)閉調(diào)試模式 'SHOW_PAGE_TRACE'=>true, ); ?>
2、創(chuàng)建控制器
?php header("Content-Type:text/html; charset=utf-8"); //設(shè)置頁面編碼格式 class IndexAction extends Action{ public function index(){ $db = M('User'); // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴 $select = $db->select(); // 查詢數(shù)據(jù) $this->assign('select',$select); // 模板變量賦值 $this->display(); // 指定模板頁 } public function type(){ $dba = M('Type'); // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴 $select = $dba->select(); // 查詢數(shù)據(jù) $this->assign('select',$select); // 模板變量賦值 $this->display('type'); // 指定模板頁 } } ?>
3、創(chuàng)建入口文件
?php define('THINK_PATH', '../ThinkPHP'); //定義ThinkPHP框架路徑(相對于入口文件) define('APP_NAME', 'App'); //定義項(xiàng)目名稱 define('APP_PATH', './App'); //定義項(xiàng)目路徑 require(THINK_PATH."/ThinkPHP.php"); //加載框架入口文件 App::run(); //實(shí)例化一個網(wǎng)站應(yīng)用實(shí)例 ?>
4、創(chuàng)建模板文件
!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">nbsp;{$user.id}/td> td bgcolor="#FFFFFF">nbsp;{$user.user}/td> td bgcolor="#FFFFFF">nbsp;{$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">nbsp;{$type.id}/td> td bgcolor="#FFFFFF">nbsp;{$type.typename}/td> td bgcolor="#FFFFFF">nbsp;{$type.dates}/td> /tr> /volist> /table> /body> /html>
二 運(yùn)行結(jié)果
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
標(biāo)簽:興安盟 濱州 南京 太原 南昌 株洲 曲靖 白酒營銷
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作示例》,本文關(guān)鍵詞 ThinkPHP,框架,基于,PDO,方式,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。