Can I store a JavaScript object in mySQL database?

I collect data from a web page visitor and put it in the JavaScript object that I create. But later I want to be able to reference the data that they entered.

I have access to a MySQL database, so is there a way to save this object there?

I want to try to save it in an object format, rather than breaking it into separate parts.

+4
source share
4 answers

Save the version of the JSON.stringified object of your object in the database, then JSON.parse when you want to return the object back. It will look something like this:

 var myObj = {some: data, other: stuff}; var myObjString = JSON.stringify(myObj); // store string in mySQL database here // load string from database var myJSONString = //mySQL database call var myLoadedObj = JSON.parse(myJSONString); 
+11
source

No, you cannot, at least not as you are.

Perhaps you can serialize it to a string. If it consists entirely of arrays, simple objects, and other types of data supported by the JSON format (which is likely to do if it is data provided by the user (the main exception if the binaries are loaded)), then you can use the JSON serializer to do this. Modern browsers provide a JSON object for this purpose, and json2.js will be a polyfill for older browsers. Then you can save the row in the database.

Without breaking it down into separate parts, it discards many of the benefits of using a relational database (i.e. relationships and the ability to perform useful searches).

+9
source

Take a look at JSON. You can use something like jQuery to serialize form data and submit it to the server. You can also just use plain old forms and handle server-side input.

0
source

Just save it as a JSON object in the field using the varchar or text data type.

0
source

Source: https://habr.com/ru/post/1414003/


All Articles