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.
TEXT data objects, as their namesake implies, are useful for storing long-form text strings in a MySQL database.
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.
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.
Remember that JavaScript stores objects and functions in the heap. Primitive values and references are stored in the stack.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With