栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP tp5中使用原生sql查询代码实例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP tp5中使用原生sql查询代码实例

注意事项:

1.先在database.php中配置好数据库

2.只要是数据库操作必须引用 use/think/Db;严格区分大小写。

下面是方法:

 public function hello5()
  {
    //所有查询必须 use/think/Db;
     
    
    
    //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中加入配置
    //例子:
    
    //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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/38135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号