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);

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I'm pretty sure it has to be surrounded with quotes, ie.,

SELECT first,last, number from friend WHERE id IN ('1','2','3','4')

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);

end quote.

You can't do it this way because $friend is an array. So, when you execute the query, it will be,

$query = "SELECT first, last, number
      FROM friend
      WHERE id IN 'array'";

which is wrong. You can use implode function to join the array.

$friend_implode = implode(",",$friend);

Then use it in your query.

$query = "SELECT first, last, number
      FROM friend
      WHERE id IN (".$friend_implode.")";

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.