1

Please help me How can I get my expected result and thanks in advance plus sorry for my weak English.

PHP:

  $dog = implode(',', $data['dogbreed']);
  $query .= "AND `dog_breeds`.`id` IN ('".$dog."')";

OUTPUT:

AND `dog_breeds`.`id` IN ('9,31')

EXPECTED RESULT:

AND `dog_breeds`.`id` IN ('9','31')

Array:

([0] => 9 [1] => 31)
1
  • Of course, you should really be using parametrised queries, which in the case of IN() are rather tricky things to construct. :-( Commented Apr 28, 2017 at 7:18

3 Answers 3

2

Try this:-

$dog = implode("','", $data['dogbreed']);
$query .= "AND `dog_breeds`.`id` IN ('".$dog."')";
Sign up to request clarification or add additional context in comments.

1 Comment

Remove that . which exist before =.
0

Change your line:-

$dog = implode(',', $data['dogbreed']);

to:-

$dog = implode("','", $data['dogbreed']);

And your code will work fine.(don't change anything else)

Check output here:- https://eval.in/784234

Note:-

Please don't change this line:-

$query .= "AND `dog_breeds`.`id` IN ('".$dog."')";

2 Comments

@shahrushabh, please pay attention. You must put the extra quotes in the first argument of implode. Don't waste people's time by trying with code that is not exactly as provided to you.
@shahrushabh please read my answer properly. and you will get the answer
0

Here is the change. Try this

$data['dogbreed']   =   array(9,10);
$dog = implode('","', $data['dogbreed']);
print  $query = 'AND dog_breeds.id IN ("'.$dog.'")';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.