PHP: Show 3 records from MySQL (exampel: ID: 5 22 87)

Discussion in 'Programming/Scripts' started by edge, Dec 11, 2009.

  1. edge

    edge HowtoForge Supporter

    I'm feeling so stupid at the moment, but what's the best way to get MySQL to show lets say 3 records.

    Example: I want to show record ID 5, 22 and 87 in on go.

    Sure I can do a query loop on each record like this
    Code:
    $query_rsFiles = "SELECT * FROM products WHERE `ID` = '$the_ID'";
    or get MySQL to do a query on all records , and when the ID is 5 or 22 or 87 are up to show the data, but this looks like a long and not proper way to do it.

    Anyone here who might know the correct way of doing this?

    Once again.. Sorry for asking this... I know.. I should know things like this.
     
  2. edge

    edge HowtoForge Supporter

    Nevermind..

    Found it I think..

    $query_rsFiles = "SELECT * FROM products WHERE `ID` = '5' OR `ID` = '22' OR `ID` = '87'";

    Not sure if OR is the best way to do it.
     
  3. id10t

    id10t Member

    select * from table where id=('5','22','87')

    You aren't really playing with PHP at this point, you are playing wtih SQL statements.
     
  4. topdog

    topdog HowtoForge Supporter

    actually is it
    Code:
    select * from table where id IN (5,22,87)
    as id is most likely a numeric value not a string.
     
  5. edge

    edge HowtoForge Supporter

    The OR that I'm using at the moment is working fine, but for sure I will give IN also a go,
    And yes.. The ID is a numeric value.

    Thank you both for your answer.
     

Share This Page