SQL LIMIT只需要一半或更少的指示

时间:2017-02-22 04:55:22

标签: php sql json

 public function getNewItems(){
     $sql = 'SELECT items.item_id, items.sell_title,items.sell_price,items.user_id,image.file, 
     user.picture, user.username FROM tbl_uploads items, img_uploads image, user user
    WHERE items.dp = 0 AND items.sold = 0 AND image.item_id = items.item_id AND user.user_id = items.user_id
    ORDER BY items.item_id DESC LIMIT :limit';
     $query = $this -> conn -> prepare($sql);
     $limit = 20;
     $query->bindValue(':limit', $limit, PDO::PARAM_INT);
     $query->execute();
     $previous_id = 0;
      while($data = $query->fetch(PDO::FETCH_OBJ)){     
            if ($previous_id == $data->item_id){
                $previous_id = $data -> item_id;
                continue;
            }
            $previous_id = $data -> item_id;
            $rating=0;
            $sql1='SELECT COUNT(*) FROM feedback WHERE seller_id = :seller_id';
            $query1 = $this -> conn -> prepare($sql1);
            $query1 -> execute(array(':seller_id' => $data->user_id));
            $number_of_rows = $query1->fetchColumn(); 
            if ($number_of_rows > 0) {
            $a=0;
            $sql1='SELECT rating FROM feedback WHERE seller_id = :seller_id';
            $query1 = $this -> conn -> prepare($sql1);
            $query1 -> execute(array(':seller_id' => $data->user_id));
                 while ($row=$query1->fetch(PDO::FETCH_ASSOC)){
                      $a++;
                      $rating=$rating+$row['rating'];
                 }
            $rating=$rating/$a;
            }
            $data->rating = "$rating";
            $items[] = $data;

        }
    return $items;
 }

所以我有这个查询应该得到20行,但它得到一半甚至小于限制。我在postman上运行它,它只有8行。

{   "result": "success",   "message": "items found",   "items": [
{
  "item_id": "2336",
  "sell_title": "LOW DENSITY MAT 24MM X 1M2 ",
  "sell_price": "550.00",
  "user_id": "433",
  "file": "5865-3.1.jpg",
  "picture": null,
  "username": "Blue",
  "rating": "0"
},
{
  "item_id": "2335",
  "sell_title": "IPHONE SE 16GB SPACE GRAY",
  "sell_price": "17900.00",
  "user_id": "433",
  "file": "5864-2.4.jpg",
  "picture": null,
  "username": "Blue",
  "rating": "0"
},
{
  "item_id": "2334",
  "sell_title": "EXECUTIVE CHAIR",
  "sell_price": "3500.00",
  "user_id": "433",
  "file": "5860-1.1.jpg",
  "picture": null,
  "username": "Blue",
  "rating": "0"
},
{
  "item_id": "2333",
  "sell_title": "RECORD & CASSETE PLAYER",
  "sell_price": "2500.00",
  "user_id": "431",
  "file": "5857-15.4.jpg",
  "picture": null,
  "username": "agreed",
  "rating": "0"
},
{
  "item_id": "2332",
  "sell_title": "RUBBERMAID DIAPER CHANGING",
  "sell_price": "22000.00",
  "user_id": "431",
  "file": "5853-14.1.jpg",
  "picture": null,
  "username": "agreed",
  "rating": "0"
},
{
  "item_id": "2330",
  "sell_title": "DINAX SLIMMING CAPSULE",
  "sell_price": "3999.00",
  "user_id": "431",
  "file": "5848-13.2.jpg",
  "picture": null,
  "username": "agreed",
  "rating": "0"
},
{
  "item_id": "2329",
  "sell_title": "CHITOSAN FOOD PURIFIER ",
  "sell_price": "450.00",
  "user_id": "431",
  "file": "5845-12.1.jpg",
  "picture": null,
  "username": "agreed",
  "rating": "0"
},
{
  "item_id": "2328",
  "sell_title": "ROTEL RA12 INTEGRATEDAMPLIFIER",
  "sell_price": "38880.00",
  "user_id": "431",
  "file": "5843-11.2.jpg",
  "picture": null,
  "username": "agreed",
  "rating": "0"
}   ] }

这是代码中的JSON。原谅凌乱的代码,我打算一旦按预期工作就修复它。

0 个答案:

没有答案