Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I store a JavaScript Object in a mySQL database?

I am gathering data from a webpage visitor and putting it into a JavaScript object I create. But later I want to be able to reference the data they entered.

I have access to a MySQL database, so is there a way for me to store this object there?

I want to try and keep it in the object format instead of breaking it up into its separate parts.

like image 852
hartste90 Avatar asked May 23 '12 18:05

hartste90


People also ask

Can you store object in MySQL database?

TEXT data objects, as their namesake implies, are useful for storing long-form text strings in a MySQL database.

Is JavaScript compatible with MySQL?

JavaScript is a client-side language that runs in the browser and MySQL is a server-side technology that runs on the server. You need to use the server-side language Node. js to connect to the MySQL Database.

Can we store JSON object in MySQL?

MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.

How are JavaScript objects stored?

Remember that JavaScript stores objects and functions in the heap. Primitive values and references are stored in the stack.


1 Answers

Store a JSON.stringified version of your object in the database, then JSON.parse it when you want the object back again. It would 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);
like image 158
Elliot Bonneville Avatar answered Sep 18 '22 13:09

Elliot Bonneville