本文實例講述了ThinkPHP連接數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:
一 代碼
1、完成入口函數(shù)的編寫
?php define('THINK_PATH', '../ThinkPHP'); //定義ThinkPHP框架路徑(相對于入口文件) define('APP_NAME', 'App'); //定義項目名稱 define('APP_PATH', './App'); //定義項目路徑 require(THINK_PATH."/ThinkPHP.php"); //加載框架入口文件 App::run(); //實例化一個網(wǎng)站應用實例 ?>
2、完成控制器的編寫
?php header("Content-Type:text/html; charset=utf-8"); //設置頁面編碼格式 class IndexAction extends Action{ public function index(){ $db_dsn="mysql://root:root@127.0.0.1:3306/db_database30"; //定義DSN $db = new Db(); //執(zhí)行類的實例化 $conn=$db->getInstance($db_dsn); //連接數(shù)據(jù)庫,返回數(shù)據(jù)庫驅動類 $select=$conn->query('select * from think_user'); //執(zhí)行查詢語句 $this->assign('select',$select); // 模板變量賦值 $this->display(); // 指定模板頁 } public function type(){ $dsn = array( 'dbms' => 'mysql', 'username' => 'root', 'password' => 'root', 'hostname' => 'localhost', 'hostport' => '3306', 'database' => 'db_database30' ); $db = new Db(); $conn=$db->getInstance($dsn); //連接數(shù)據(jù)庫,返回數(shù)據(jù)庫驅動類 $select=$conn->query('select * from think_type'); //執(zhí)行查詢語句 $this->assign('select',$select); // 模板變量賦值 $this->display('type'); // 指定模板頁 } } ?>
3、完成模板編寫
!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>
二 運行結果
更多關于thinkPHP相關內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。