Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Distinct & Limit Not Working

    Hi guys,

    DB is mysql.
    I have the below query which i'm trying to run through PHP.

    PHP Code:
    $sql "SELECT DISTINCT pid FROM campaign_details WHERE camp_id='$camp_id' LIMIT 7";
    $newquery mysql_query($sql) or die(mysql_error()); 
    There are 10 distinct pid values but I want only 7.
    The stupid thing returns all of them when I need to limit them at 7.

    Any work around this?
    Or Do I need to go and take a coffee break and have another look? haha.

    Cheers

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    Ummm...so just limit it to 7 in the PHP code.

    Code:
    $cnt = 7;
    while ( ...fetch one row ... )
    {
        ...
        if ( --$cnt == 0 ) break;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    But now that I look at your query again, I don't see why it isn't working.

    Try running the query *OUTSIDE* of PHP and see what you get.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •