skymvc开发手册之查询数据方法
skymvc查询数据方法
一、获取列表数据
1.select($option=array(),&$rscount=false) 推荐使用
select($option=array(),&$rscount=false) $option=array( "where"=>$where, "order"=>$order, "start"=>$start, "limit"=>$limit, "fields"=>"*" ); $where==>查询条件 $order==>查询排序 $start==>开始 $limit==>行数 $data==>数据 $rscount=true or false;
$start=get("per_page",'i'); $limit=12; $option=array( "where"=>" status=1 ", "order"=>" id DESC ", "start"=>$start, "limit"=>$limit, "fields"=>"id,title,status" ); $rscount=true; $list=M("article")->select($option,$rscount); //分页 $url="/index.php?m=article"; $pagelist=$this->pagelist($rscount,$limit,$url);
2.getAll($sql) 推荐在复杂sql查询中使用
$start=get("per_page",'i'); $limit=12; $where=" status=1 " ; $sql="select * from ".table("article")." where $where limit $start,$limit "; $list=M("article")->getAll($sql); $rscount=M("article")->getCount($where); //分页 $url="/index.php?m=article"; $pagelist=$this->pagelist($rscount,$limit,$url);
二、查询单列数据
selectCols($option=array(),&$rscount=false) 推荐使用
2.getCols($sql);
参数同一
$start=get("per_page",'i'); $limit=12; $option=array( "where"=>" status=1 ", "order"=>" id DESC ", "start"=>$start, "limit"=>$limit, "fields"=>"id" ); $rscount=false; $ids=M("article")->selectCols($option,$rscount); print_r($ids);
三、查询单行数据
1、selectRow($ops);
$start=get("per_page",'i'); $option=array( "where"=>" status=1 ", "order"=>" id DESC ", "start"=>$start, "limit"=>1, "fields"=>"id,title,status" ); $row=M("article")->selectRow($option); //option作为where使用 $where=" status=1 "; $row=M("article")->selectRow($where);
2、getRow($sql);
$sql="select * from ".table("article")." where status=1 limit 1 "; $list=M("article")->getRow($sql);
四、查询单个字段
1、selectOne($ops);
$start=get("per_page",'i'); $option=array( "where"=>" status=1 ", "order"=>" id DESC ", "start"=>$start, "limit"=>1, "fields"=>"id" ); $id=M("article")->selecOne($option);
2、getOne($sql);
$sql="select id from ".table("article")." where status=1 limit 1 "; $id=M("article")->getRow($sql);
五、获取统计getCount
$rscount=M("article")->getCount("status=1 ");