最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ThinkPHP連接數(shù)據(jù)庫操作示例【基于DSN方式和數(shù)組傳參的方式】

 更新時間:2018年03月23日 11:32:29   作者:chengqiuming  
這篇文章主要介紹了ThinkPHP連接數(shù)據(jù)庫操作,結(jié)合實例形式分析了thinkPHP基于DSN方式和數(shù)組傳參的方式進行數(shù)據(jù)庫連接的實現(xiàn)步驟與屬性設(shè)置、控制器、模板使用等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了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)站應(yīng)用實例
?>

2、完成控制器的編寫

<?php
header("Content-Type:text/html; charset=utf-8");  //設(shè)置頁面編碼格式
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ù)庫驅(qū)動類
    $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ù)庫驅(qū)動類
    $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>

二 運行結(jié)果

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

平乡县| 杭锦后旗| 互助| 新安县| 阳春市| 天台县| 凤城市| 崇阳县| 化州市| 宁武县| 开平市| 伊春市| 三原县| 微山县| 通渭县| 长春市| 东宁县| 南昌县| 扶风县| 精河县| 蒙城县| 洛宁县| 平邑县| 昌邑市| 济阳县| 石门县| 万源市| 电白县| 华安县| 明星| 淮滨县| 三台县| 汶川县| 泰州市| 新昌县| 白玉县| 左权县| 海安县| 高雄市| 东莞市| 北海市|