PHP tp5中使用原生sql查詢代碼實(shí)例
注意事項:
1.先在database.php中配置好數(shù)據(jù)庫
2.只要是數(shù)據(jù)庫操作必須引用 use/think/Db;嚴(yán)格區(qū)分大小寫。
下面是方法:
public function hello5()
{
//所有查詢必須 use/think/Db;
/* 1 配置數(shù)據(jù)庫
* 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);
// 查詢數(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ù)庫列表
// $result = Db::query('show tables from tpshop1');
// print_r($result);
// 清空數(shù)據(jù)表
// $result = Db::execute('TRUNCATE table sb_ad');
// dump($result);
/**************多個數(shù)據(jù)庫操作************/
//在application/config.php中加入配置
//例子:
/*
* // 數(shù)據(jù)庫配置1
'db2' => [
// 數(shù)據(jù)庫類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫名
'database' => 'tpshop2',
// 數(shù)據(jù)庫用戶名
'username' => 'root',
// 數(shù)據(jù)庫密碼
'password' => '',
// 數(shù)據(jù)庫連接端口
'hostport' => '',
// 數(shù)據(jù)庫連接參數(shù)
'params' => [],
// 數(shù)據(jù)庫編碼默認(rèn)采用utf8
'charset' => 'utf8',
// 數(shù)據(jù)庫表前綴
'prefix' => 'tp_',
],
依次類推
*/
//connect為鏈接數(shù)據(jù)庫
// $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ù)庫對象
// $db2 = Db::connect('db2');獲取數(shù)據(jù)庫對象然后再操作
// $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);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Laravel中獲取路由參數(shù)Route Parameters的五種方法示例
這篇文章主要給大家介紹了關(guān)于Laravel中獲取路由參數(shù)Route Parameters的五種方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Laravel具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09
php冒泡算法實(shí)現(xiàn)倒序和正序排列的示例代碼
冒泡排序是一種簡單的排序算法,其主要思想是比較相鄰的兩個元素,根據(jù)需要交換位置,將較大(或較?。┑脑刂饾u冒泡到數(shù)組的一端,從而實(shí)現(xiàn)排序,這篇文章主要介紹了php冒泡算法實(shí)現(xiàn)倒序和正序排列,需要的朋友可以參考下2023-11-11
PHP receiveMail實(shí)現(xiàn)收郵件功能
這篇文章主要為大家詳細(xì)介紹了PHP receiveMail實(shí)現(xiàn)收郵件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
thinkPHP實(shí)現(xiàn)的聯(lián)動菜單功能詳解
這篇文章主要介紹了thinkPHP實(shí)現(xiàn)的聯(lián)動菜單功能,結(jié)合實(shí)例形式分析了thinkPHP聯(lián)動菜單的實(shí)現(xiàn)步驟與具體操作技巧,需要的朋友可以參考下2017-05-05
php示例詳解Constructor Prototype Pattern 原型模式
原型模式是一種創(chuàng)建者模式,其特點(diǎn)在于通過“復(fù)制”一個已經(jīng)存在的實(shí)例來返回新的實(shí)例,而不是新建實(shí)例。2015-10-10
yii框架通過控制臺命令創(chuàng)建定時任務(wù)示例
這篇文章主要介紹了yii框架通過控制臺命令創(chuàng)建定時任務(wù)示例,需要的朋友可以參考下2014-04-04
smarty模板引擎中內(nèi)建函數(shù)if、elseif和else的使用方法
這篇文章主要介紹了smarty模板引擎中內(nèi)建函數(shù)if、elseif和else的使用方法,通過兩個示例分析了if、elseif和else的使用技巧,需要的朋友可以參考下2015-01-01

