Select where clause with "IN"

Discussion in 'PHP' started by assgar, May 25, 2008.

  1. #1
    Hi

    I would like to put a list of ids in an array
    and loop to select data from a table using
    "IN" in the where clause.

    I am not having any success getting this to work.
    Any suggestions?


    Note:I am using Mysql database, apache, linux/Windows


    
    <?
    //array with list of friends to display
    $friend = array('1','2','3','4');
    
    $query = "SELECT first, last, number
    	  FROM friend
    	  WHERE id IN '$friend'";
    $result = mysqli_query($mysqli, $query);
    
    
    
    PHP:
     
    assgar, May 25, 2008 IP
  2. Awesome Ninja

    Awesome Ninja Peon

    Messages:
    141
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?
    //array with list of friends to display
    $friend = array('1','2','3','4');
    $arrayl = count($friend);
    
    $i = 0;
    while($i < $arrayl) {
    
    $query = "SELECT first, last, number FROM friend WHERE `id` = '$friend[$i]'";
    $result = mysqli_query($mysqli, $query);
    $i++;
    }
    
    ?>
    PHP:
    Try that, is that what your looking for?
     
    Awesome Ninja, May 25, 2008 IP
  3. aminemx

    aminemx Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $friend = array('1','2','3','4');

    $query = "SELECT first, last, number
          FROM friend
          WHERE id IN (".implode(',',$friend).")";
    $result = mysqli_query($mysqli, $query);
    PHP:
     
    aminemx, May 26, 2008 IP
  4. assgar

    assgar Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks this worked.
     
    assgar, May 31, 2008 IP