tp查询数据方式

<?php
/**
 * Created by PhpStorm.
 * Date: 2017/5/8
 * Time: 下午2:15
 */
 
namespace app\api\model;
 
 
use think\Db;
use think\Exception;
 
class Banner
{
    public static function getBannerByID($id){
        //1.使用原生sql
//        $result = Db::query('select * from banner_item where banner_id=?',[$id]);
//        return $result;
        //2.使用查询构建器
        /*
         * 链式查询Db::table('banner_item')->where('banner_id','=',$id) 返回查询对象,->select();返回查询结果,
         * 除了select操作还有 find(返回一条数据) update delete insert
         * 对应的where 也分三种,1.表达式where('字段名','表达式','查询条件') 2.数组发 3.闭包。
         */
 
        // 2.1 表达式法
//        $result = Db::table('banner_item')
//            ->where('banner_id','=',$id)
//            ->select();
//        return $result;
        //2.2 闭包法
        $result = Db::table('banner_item')
            ->where(function ($query) use($id){
                $query->where('banner_id','=',$id);
 
            })
            ->select();
        return $result;
 
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_34146679/article/details/81297700
今日推荐