query help

Discussion in 'PHP' started by PinoyIto, Aug 5, 2006.

  1. #1
    I want to display 5 records per category in my database. How can I query this?
     
    PinoyIto, Aug 5, 2006 IP
  2. [*-AnOnYmOuS-*]

    [*-AnOnYmOuS-*] Active Member

    Messages:
    253
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    [*-AnOnYmOuS-*], Aug 5, 2006 IP
  3. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #3
    an idea
    
    $sql = "select * from category";
    $query = mysql_query($sql);
    while($row = mysql_fetch_assoc($query)) {
    for($i=1;$i<6;$i++) {
    $sql2 = "select * from record";
    $query2 = mysql_query($sql2);
    $row2 = mysql_fetch_assoc($query2);
    }
    }
    
    PHP:
     
    sandossu, Aug 5, 2006 IP
  4. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #4
    That solution will slowdown the database... is there a a way to use sub query for this problem...

    actually I am currently using this code...
    
    $sql = "select category from table group by category";
    $rec = mysql_query($sql) or die(mysql_error());
    $datas = mysql_fetch_array($rec);
    
    do{
    $sq = "select * from table where category = '$datas[category]' limit 5";
    $rst = mysql_query($sq) or die(mysql_error());
    $datas1 = mysql_fetch_array($rst);
      do{
       echo $datas1[field1];
         }while($datas1 = mysql_fetch_array($rst));
    
    }while($datas = mysql_fetch_array($rec));
    
    Code (markup):
    The problem with this code is very slow and use to much resources of server... which is not wise
     
    PinoyIto, Aug 5, 2006 IP
  5. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #5
    SELECT * FROM category LIMIT 0,5
    
    Code (markup):
    Selects the first 5 records it finds.
     
    danielbruzual, Aug 5, 2006 IP
  6. Boby

    Boby Peon

    Messages:
    207
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This way ressources are minimum...

    $sql = "SELECT * FROM `category` LIMIT 5";
    PHP:
    or if you know the start point:
    $sql = "SELECT * FROM `category` LIMIT 80, 5";
    PHP:
     
    Boby, Aug 5, 2006 IP
  7. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #7
    thanks but what I want is to make the code below a better query... a single query which is recomended than query inside loop. this will use to much resources.

    $sql = "select category from table group by category";
    $rec = mysql_query($sql) or die(mysql_error());
    $datas = mysql_fetch_array($rec);
    
    do{
    $sq = "select * from table where category = '$datas[category]' limit 5";
    $rst = mysql_query($sq) or die(mysql_error());
    $datas1 = mysql_fetch_array($rst);
      do{
       echo $datas1[field1];
         }while($datas1 = mysql_fetch_array($rst));
    
    }while($datas = mysql_fetch_array($rec));
    
    Code (markup):
     
    PinoyIto, Aug 5, 2006 IP