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

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ù)據(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>關(guān)于使用thinkPHP5框架如何連接數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
文章題目:使用thinkPHP5框架如何連接數(shù)據(jù)庫-創(chuàng)新互聯(lián)
URL標題:http://www.chinadenli.net/article36/dcpspg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、動態(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)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容