久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP tp5中使用原生sql查詢(xún)代碼實(shí)例

瀏覽:125日期:2022-09-08 18:19:36

注意事項(xiàng):

1.先在database.php中配置好數(shù)據(jù)庫(kù)

2.只要是數(shù)據(jù)庫(kù)操作必須引用 use/think/Db;嚴(yán)格區(qū)分大小寫(xiě)。

下面是方法:

public function hello5() { //所有查詢(xún)必須 use/think/Db; /* 1 配置數(shù)據(jù)庫(kù) * 2 使用DB 命名空間 * */ /****************tp5中使用原生語(yǔ)句*******************/ //query 用于查詢(xún) 其他的用execute// 插入記錄// $result = Db::execute(’insert into sb_ad (ad_name, ad_content ,status) values (1, '456',1)’);// dump($result); // 更新記錄// $result = Db::execute(’update sb_ad set ad_name = 'framework' where ad_id = 1 ’);// dump($result);// 查詢(xún)數(shù)據(jù)// $result = Db::query(’select * from sb_ad where ad_id = 1’);// print_r($result); // 刪除數(shù)據(jù)// $result = Db::execute(’delete from sb_ad where ad_id = 2 ’);// dump($result); //其它操作 // 顯示數(shù)據(jù)庫(kù)列表// $result = Db::query(’show tables from tpshop1’);// print_r($result);// 清空數(shù)據(jù)表// $result = Db::execute(’TRUNCATE table sb_ad’);// dump($result); /**************多個(gè)數(shù)據(jù)庫(kù)操作************/ //在application/config.php中加入配置 //例子: /* * // 數(shù)據(jù)庫(kù)配置1’db2’ => [ // 數(shù)據(jù)庫(kù)類(lèi)型 ’type’ => ’mysql’, // 服務(wù)器地址 ’hostname’ => ’127.0.0.1’, // 數(shù)據(jù)庫(kù)名 ’database’ => ’tpshop2’, // 數(shù)據(jù)庫(kù)用戶(hù)名 ’username’ => ’root’, // 數(shù)據(jù)庫(kù)密碼 ’password’ => ’’, // 數(shù)據(jù)庫(kù)連接端口 ’hostport’ => ’’, // 數(shù)據(jù)庫(kù)連接參數(shù) ’params’ => [], // 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8 ’charset’ => ’utf8’, // 數(shù)據(jù)庫(kù)表前綴 ’prefix’ => ’tp_’,],依次類(lèi)推 */ //connect為鏈接數(shù)據(jù)庫(kù)// $result = Db::connect(’db2’)->query(’select * from sb_ad where ad_id = 1’);// print_r($result);// $result = Db::connect(’db3’)->query(’select * from sb_ad where ad_id = 1’); // print_r($result); // $db1 = Db::connect(’db1’);獲取數(shù)據(jù)庫(kù)對(duì)象// $db2 = Db::connect(’db2’);獲取數(shù)據(jù)庫(kù)對(duì)象然后再操作// $db1->query(’select * from sb_ad where ad_id = 1’);// $db2->query(’select * from sb_ad where ad_id = 1’); /*****參數(shù)綁定******/// Db::execute(’insert into sb_ad (ad_name, ad_content ,status) values (?, ?, ?)’, [3, ’thinkphp’, 1]);// $result = Db::query(’select * from sb_ad where ad_id = ?’, [3]);// print_r($result); /******命名占位符綁定*****/// Db::execute(’insert into sb_ad (ad_name, ad_content ,status) values (:ad_name, :ad_content, :status)’, [’ad_name’ => 11, ’ad_content’ => ’thinkphp’, ’status’ => 1]);// $result = Db::query(’select * from sb_ad where ad_id=:id’, [’id’ => 10]);// print_r($result);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 芷江| 博乐市| 徐闻县| 绥芬河市| 南涧| 阿克陶县| 阳朔县| 湖南省| 河北区| 邛崃市| 育儿| 日照市| 武山县| 陆川县| 无极县| 三门县| 南开区| 驻马店市| 扶绥县| 凤冈县| 洛浦县| 伊吾县| 岑溪市| 高邑县| 普宁市| 安庆市| 石渠县| 鹿邑县| 绥阳县| 兖州市| 苍溪县| 礼泉县| 葵青区| 法库县| 视频| 南汇区| 汕头市| 射洪县| 永寿县| 太康县| 襄城县|