0

So i wanted to save some tags data to the database from the UI form,

Tags: ["male","female","kids"]

I tried everything like but it saves as a string, ialso tried checking if i can alter the data type to array in mysql, or json but i got this

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 'JSON(255) NOT NULL' at line 1

I also tried json_encode and json_decode() but still no head so please what can i do?

1
  • Pls share what you have tried! Based on an error message only it is a bit difficult to figure out what may have gone wrong. Commented Jul 21, 2016 at 23:24

2 Answers 2

2

There isn't any data type as JSON or Array in DB.
What you can do is use the tags field as TEXT as told by @Rick James and then encode your input as json by using json_encode() method before inserting and decode it after retrieving the data from DB by using json_decode() method.

JSON is basically a minimal, readable format for structuring data. Which means it can be considered as a String.

Here is a good post about JSON, in case you need. What is JSON?

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

4 Comments

@MN that's very strange. It must work on server. Is there any error or exception during usage ?
Thanks. I have this error: Invalid argument supplied for foreach()
@MN it's very hard to understand the problem from this. Though you can try printing the string before decode and after decoding, try printing the variable with var_dump()
Thanks. my problem solved with (array) json_decode() !!
0

There is no datatype called JSON. Instead use TEXT without the (255).

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.