-1
$sql= 'select * from `name` where `author_id` IN (' . implode("','", $authors_ids) . ')';

Where $authors_ids is an array.

u_mulder
  • 54,101
  • 5
  • 48
  • 64

2 Answers2

2

This could be the answer.

$sql = "select * from `name` where `author_id` IN ('" . implode("','", $authors_ids) . "')";

But as mentioned by other people in the comments why transform it to a string instead of just having it as an integer? (I am assuming the id is an integer).

$sql = 'select * from `name` where `author_id` IN (' . implode(",", $authors_ids) . ')';

NB. I am also here assuming you have done some security around your authors array before it is passed to this.

0

You are adding single quotes to elements.

But, we need to add it to end and start manually.

$authorsStr = "'" . implode ( "', '", $authors_ids ) . "'";
$sql= 'select * from `name` where `author_id` IN (' .$authorsStr . ')';

Say, you have array of authors:

$authors = ['Bob', 'Willy', 'Williams'];

In your case, sql:

select * from `name` where `author_id` IN (Bob','Willy','Williams)

Which is definitely, a syntax error.

If you add single quotes to start and end manually as above, your sql will become:

select * from `name` where `author_id` IN ('Bob','Willy','Williams')

Which does not contain any syntax error.

Pupil
  • 23,834
  • 6
  • 44
  • 66