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

您的位置:首頁技術文章
文章詳情頁

PHP tp5中使用原生sql查詢代碼實例

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

注意事項:

1.先在database.php中配置好數據庫

2.只要是數據庫操作必須引用 use/think/Db;嚴格區分大小寫。

下面是方法:

public function hello5() { //所有查詢必須 use/think/Db; /* 1 配置數據庫 * 2 使用DB 命名空間 * */ /****************tp5中使用原生語句*******************/ //query 用于查詢 其他的用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);// 查詢數據// $result = Db::query(’select * from sb_ad where ad_id = 1’);// print_r($result); // 刪除數據// $result = Db::execute(’delete from sb_ad where ad_id = 2 ’);// dump($result); //其它操作 // 顯示數據庫列表// $result = Db::query(’show tables from tpshop1’);// print_r($result);// 清空數據表// $result = Db::execute(’TRUNCATE table sb_ad’);// dump($result); /**************多個數據庫操作************/ //在application/config.php中加入配置 //例子: /* * // 數據庫配置1’db2’ => [ // 數據庫類型 ’type’ => ’mysql’, // 服務器地址 ’hostname’ => ’127.0.0.1’, // 數據庫名 ’database’ => ’tpshop2’, // 數據庫用戶名 ’username’ => ’root’, // 數據庫密碼 ’password’ => ’’, // 數據庫連接端口 ’hostport’ => ’’, // 數據庫連接參數 ’params’ => [], // 數據庫編碼默認采用utf8 ’charset’ => ’utf8’, // 數據庫表前綴 ’prefix’ => ’tp_’,],依次類推 */ //connect為鏈接數據庫// $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’);獲取數據庫對象// $db2 = Db::connect(’db2’);獲取數據庫對象然后再操作// $db1->query(’select * from sb_ad where ad_id = 1’);// $db2->query(’select * from sb_ad where ad_id = 1’); /*****參數綁定******/// 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);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: PHP
相關文章:
主站蜘蛛池模板: 尼玛县| 敦煌市| 镶黄旗| 嵊州市| 香港 | 工布江达县| 花莲市| 东兴市| 平陆县| 苗栗县| 南川市| 虞城县| 志丹县| 榕江县| 衡阳县| 峨边| 乐清市| 柘城县| 鱼台县| 增城市| 乌兰县| 贵州省| 北票市| 吉木乃县| 凉城县| 濮阳市| 左权县| 墨脱县| 炎陵县| 芮城县| 曲阜市| 富民县| 南部县| 黄大仙区| 滕州市| 昌乐县| 西城区| 沙河市| 古丈县| 外汇| 龙门县|