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

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

Thanks it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.