使用thinkPHP5框架如何連接數(shù)據(jù)庫-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)使用thinkPHP5框架如何連接數(shù)據(jù)庫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),白塔企業(yè)網(wǎng)站建設(shè),白塔品牌網(wǎng)站建設(shè),網(wǎng)站定制,白塔網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,白塔網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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


通過配置文件來連接。。

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


  // 使用方法配置數(shù)據(jù)庫連接
  public function data1 ()
  {
    $DB = Db::connect([
      // 數(shù)據(jù)庫類型
      'type'      => 'mysql',
      // 服務(wù)器地址
      '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í)例化數(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í)例化數(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>

關(guān)于使用thinkPHP5框架如何連接數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

文章題目:使用thinkPHP5框架如何連接數(shù)據(jù)庫-創(chuàng)新互聯(lián)
URL標(biāo)題:http://muchs.cn/article36/dcpspg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、動態(tài)網(wǎng)站全網(wǎng)營銷推廣、ChatGPT關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)