0

I have an array called $friends and it has some values

$friends = array(); $friends[0] = 'Alex'; $friends[1] = 'Jake';

I want to be-able for my SQL query to select every array value in this case it is Alex and Jake

$q = "SELECT * FROM posts WHERE username='$friends' ORDER BY id DESC";

But I can't do that, I get a message saying "Notice: Array to string conversion in".

Please help I am quite new to PHP

another user
  • 93
  • 1
  • 11

1 Answers1

2

Use implode make the array into a string with the values and use IN for lists in MySQL.

$friends = array('Alex','Jake');
$str     = implode ("', '", $friends);
$q       = "SELECT * FROM posts WHERE username IN ('". $str . "') ORDER BY id DESC";
Julio Soares
  • 1,200
  • 8
  • 11