濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > PHP tp5中使用原生sql查詢代碼實(shí)例

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

熱門標(biāo)簽:黔江400電話如何辦理 招標(biāo)自動(dòng)語(yǔ)音外呼系統(tǒng) OMG地圖標(biāo)注app 地圖標(biāo)注視頻廣告入駐 gps 地圖標(biāo)注軟件 電銷機(jī)器人便宜的有嗎 400電話鄭州申請(qǐng) 中原區(qū)電話機(jī)器人價(jià)格 ai電話機(jī)器人加盟代理

注意事項(xiàng):

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

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

下面是方法:

 public function hello5()
  {
    //所有查詢必須 use/think/Db;
    /* 1 配置數(shù)據(jù)庫(kù)
     * 2 使用DB 命名空間
     * 
     */ 
    
    /****************tp5中使用原生語(yǔ)句*******************/
    //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ù)庫(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ù)類型
          'type'   => 'mysql',
          // 服務(wù)器地址
          'hostname' => '127.0.0.1',
          // 數(shù)據(jù)庫(kù)名
          'database' => 'tpshop2',
          // 數(shù)據(jù)庫(kù)用戶名
          '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_',
        ],
        依次類推
     */
    //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í)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • tp5.1 框架數(shù)據(jù)庫(kù)-數(shù)據(jù)集操作實(shí)例分析
  • tp5.1 框架路由操作-URL生成實(shí)例分析
  • tp5.1 框架join方法用法實(shí)例分析
  • tp5.1框架數(shù)據(jù)庫(kù)子查詢操作實(shí)例分析
  • tp5.1 框架數(shù)據(jù)庫(kù)常見操作詳解【添加、刪除、更新、查詢】
  • TP5多入口設(shè)置實(shí)例講解

標(biāo)簽:哈密 孝感 北京 阿里 池州 日照 那曲 濟(jì)源

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP tp5中使用原生sql查詢代碼實(shí)例》,本文關(guān)鍵詞  PHP,tp5,中,使用,原生,sql,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP tp5中使用原生sql查詢代碼實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PHP tp5中使用原生sql查詢代碼實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    延安市| 广平县| 高要市| 革吉县| 历史| 商南县| 阳朔县| 宜都市| 孟津县| 平潭县| 望谟县| 克什克腾旗| 昔阳县| 四会市| 麻栗坡县| 柏乡县| 云霄县| 巨野县| 石渠县| 峨山| 沧州市| 磐安县| 祁阳县| 尼木县| 宜阳县| 黔江区| 崇左市| 玛纳斯县| 迁安市| 灯塔市| 泾川县| 修文县| 樟树市| 陵水| 垫江县| 黎川县| 长武县| 治县。| 汉阴县| 买车| 南皮县|