0

I have a MySQL 5.7 table which includes a column of type VARCHAR named 'area' which contains JSON data. For example ["BS20","BS21"]

I need to search on that table to find the first row containing a particular string. The string is contained in a variable like

$area = BS20;

and I have tried this for my query

SELECT * FROM aedv2_admin WHERE JSON_CONTAINS(area,'$area')=1

and I get an error

Invalid JSON text in argument 2 to function json_contains: "Invalid value." at position 0.12

For testing I have tried hard coding argument 2 as BS20 and 'BS20' but neither helps. What am I doing wrong?

2
  • 1
    and I have tried this for my query Provide query text, not a part cut off the PHP code. Commented May 4, 2021 at 17:42
  • Use JSON_SEACRCH(). Or '$area' after the substitution must give '"BS20"'. dbfiddle.uk/… Commented May 4, 2021 at 17:49

1 Answer 1

1

Phew! That was a bit painful, but with thanks to Akina my final code ended up like this

$area = '\"'.$_POST['area'].'\"';

$query_sponsor = "SELECT * FROM aedv2_admin WHERE JSON_CONTAINS(area,'$area')";
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.