Blogger Information
Blog 14
fans 0
comment 0
visits 16109
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库的源生查询
依然很m丶的博客
Original
1210 people have browsed it

源生查询:

<?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);
    }
}




Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post