這篇文章將為大家詳細(xì)講解有關(guān)使用thinkPHP5框架如何連接數(shù)據(jù)庫(kù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
1、配置文件目錄 tp5\application\database.php
通過(guò)配置文件來(lái)連接。。
在控制器里方法鏈接數(shù)據(jù)庫(kù) ;查詢時(shí)寫法 和使用系統(tǒng)的DB類方法略有差異
// 使用方法配置數(shù)據(jù)庫(kù)連接 public function data1 () { $DB = Db::connect([ // 數(shù)據(jù)庫(kù)類型 'type' => 'mysql', // 服務(wù)器地址 'hostname' => '127.0.0.1', // 數(shù)據(jù)庫(kù)名 '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ù)庫(kù)
控制器下文件(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 '上課來(lái)'; return $this -> fetch(); } // 使用配置文件連接數(shù)據(jù)庫(kù) public function data() { // 實(shí)例化數(shù)據(jù)庫(kù)系統(tǒng)類 $DB = new Db; // 查詢數(shù)據(jù),表名為uu的所有數(shù)據(jù) $data = $DB::table("uu") -> select(); // 使用sql語(yǔ)句 //$data = $DB::query("select * from uu"); dump($data); } }
http://yourwebname/public/index.php/index/Index/data 獲取數(shù)據(jù)打印測(cè)試
3.將數(shù)據(jù)渲染模板頁(yè)面
<?php namespace app\index\controller; use think\Db; use think\Controller; // 使用model連接數(shù)據(jù)庫(kù)要引入moadel use think\Model; class Index extends Controller { public function index() { // return 's'; $this -> data(); return $this -> fetch(); } // 使用系統(tǒng)配置文件連接數(shù)據(jù)庫(kù) public function data() { // 實(shí)例化數(shù)據(jù)庫(kù)系統(tǒng)類 $DB = new Db; // 查詢數(shù)據(jù) $data = $DB::table("uu") -> select(); $this -> assign("user",$data); // dump($data); } }
4.模板頁(yè)面即可引用渲染數(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ù)庫(kù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章題目:使用thinkPHP5框架如何連接數(shù)據(jù)庫(kù)-創(chuàng)新互聯(lián)
URL標(biāo)題:http://chinadenli.net/article36/dcpspg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、動(dòng)態(tài)網(wǎng)站、全網(wǎng)營(yíng)銷推廣、ChatGPT、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容