0

I am work in php and this is my array

Array ( [0] => Sharp [1] => New [2] => Stress [3] => Bending forward [4] => Headache )

And I Want to retrieve the record which match the array value in tag column

select * 
from table_name 
where tag In(Sharp, New,Stress,Bending forward,Headache);

But above query give the error in php side as like

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\Demo\abc.php on line 14

And mysql side is

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'forward,Headache) LIMIT 0, 25' at line 1

Thank you in advance

3
  • Missing quotes for string Commented Oct 1, 2016 at 13:35
  • but i want match all value which store in array Commented Oct 1, 2016 at 13:41
  • Check the given below answers u will get the idea Commented Oct 1, 2016 at 13:47

1 Answer 1

1

You are missing quotes for your string value.

This:

SELECT * FROM table_name WHERE tag IN (Sharp,New,Stress,Bending forward,Headache)

Should be:

SELECT * FROM table_name WHERE tag IN ('Sharp','New', 'Stress','Bending forward','Headache')

If you have an array than you can use implode()

$string = implode('","',$yourArray);

$query = 'SELECT * FROM table_name WHERE tag IN ("'.$string.'")';

One more point, as per your code error you are using mysql_* extension which is deprecated and closed in PHP 7. You can use mysqli_* or PDO.

Sign up to request clarification or add additional context in comments.

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.