博主信息
博文 14
粉丝 0
评论 0
访问量 18521
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
数据库的源生查询
依然很m丶的博客
原创
1343人浏览过

源生查询:

<?php
namespace app\index\controller;
use \think\Db;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //1.查询操作:价格大于10元的商品
       $sql= "select id,name from shop_name where price > 15";
       $result=Db::query($sql);
       dump($result);
    }
}

用命名占位符绑定参数:

  • 查询操作

<?php
namespace app\index\controller;
use \think\Db;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //1.查询操作:价格大于10元的商品,用命名占位符进行参数绑定
       $sql= "select id,name,price from shop_name where price > :price";
       $result=Db::query($sql,['price'=>15]);
       dump($result);
    }
}
  • 更改操作

<?php
namespace app\index\controller;
use \think\Db;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //更新操作,将id=2 的商品价格改成100
       $sql= "update shop_name set price='100元' where id=:id";
       $result=Db::execute($sql,['id'=>2]);
       dump($result);
    }
}
  • 添加操作

<?php
namespace app\index\controller;
use \think\Db;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //添加操作,添加 name=java高级 price=80元 time=2018-8-27 type = java
       $sql= "insert into shop_name(name,price,time,type) value(:name,:price,:time,:type)";
       $result=Db::execute($sql,['name'=>'java高级','price'=>'80元','time'=>'2018-8-27','type'=>'java']);
       dump($result);
    }
}

删除操作

<?php
namespace app\index\controller;
use \think\Db;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //添加操作,添加 name=java高级 price=80元 time=2018-8-27 type = java
       $sql= "delete from shop_name where id=:id";
       $result=Db::execute($sql,['id'=>15]);
       dump($result);
    }
}




本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学