欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例

 更新時間:2018年12月24日 10:05:03   作者:milli236  
這篇文章主要介紹了tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法,結合實例形式較為詳細的分析了基于thinkPHP5框架連接數(shù)據(jù)庫的相關配置、數(shù)據(jù)讀取、模板渲染等操作技巧,需要的朋友可以參考下

本文實例講述了thinkPHP5框架連接數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:

1、配置文件目錄 tp5\application\database.php

通過配置文件來連接。。

也可以通過方法鏈接

在控制器里方法鏈接數(shù)據(jù)庫 ;查詢時寫法 和使用系統(tǒng)的DB類方法略有差異

  // 使用方法配置數(shù)據(jù)庫連接
  public function data1 ()
  {
    $DB = Db::connect([
      // 數(shù)據(jù)庫類型
      'type'      => 'mysql',
      // 服務器地址
      'hostname'    => '127.0.0.1',
      // 數(shù)據(jù)庫名
      'database'    => 'user',
      // 用戶名
      'username'    => 'root',
      // 密碼
      'password'    => 'root',
      // 端口
      'hostport'    => '3306',
    ]);
    // dump($DB);
    // 查詢數(shù)據(jù),,,,和使用系統(tǒng)的DB類方法略有差異
    $data = $DB -> table("uu") -> select();
    dump($data);
  }

2.基本使用 、 增刪改查

控制器使用配置文件連接數(shù)據(jù)庫

控制器下文件(tp5\application\index\controller\Index.php)寫入

<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Index extends Controller
{
  public function index()
  {
    // return '上課來';
    return $this -> fetch();
  }
  // 使用配置文件連接數(shù)據(jù)庫
  public function data()
  {
    // 實例化數(shù)據(jù)庫系統(tǒng)類
    $DB = new Db;
    // 查詢數(shù)據(jù),表名為uu的所有數(shù)據(jù)
    $data = $DB::table("uu") -> select();
    // 使用sql語句
    //$data = $DB::query("select * from uu");
    dump($data);
  }
}

http://yourwebname/public/index.php/index/Index/data 獲取數(shù)據(jù)打印測試

3.將數(shù)據(jù)渲染模板頁面

<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
// 使用model連接數(shù)據(jù)庫要引入moadel
use think\Model;
class Index extends Controller
{
  public function index()
  {
    // return 's';
    $this -> data();
    return $this -> fetch();
  }
// 使用系統(tǒng)配置文件連接數(shù)據(jù)庫
  public function data()
  {
    // 實例化數(shù)據(jù)庫系統(tǒng)類
    $DB = new Db;
    // 查詢數(shù)據(jù)
    $data = $DB::table("uu") -> select();
    $this -> assign("user",$data);
    // dump($data);
  }
}

4.模板頁面即可引用渲染數(shù)據(jù)

tp5\application\index\view\index\index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>s</title>
</head>
<body>
  <div> s</div>
  {volist name="user" id="vo"}
    <a href="">{$vo.name}</a>
  {/volist}
</body>
</html>

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

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

相關文章

最新評論