Can I store a JavaScript Object in a mySQL database?

前端 未结 5 532
谎友^
谎友^ 2021-02-07 20:59

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 acce

5条回答
  •  囚心锁ツ
    2021-02-07 21:29

    No, you can't, at least not as it is.

    You might be able serialise it to a string. If it consists entirely of arrays, simple objects, and the other data types supported by the JSON format (which it probably will do if it is user supplied data (the main exception being if binary files are uploaded)), then you could use a JSON serializer to do so. Modern browsers provide the JSON object for this purpose and json2.js will polyfill for older browsers. You could then store the string in the database.

    Not breaking it into separate parts does throw away the many of the advantages of using a relational database though (i.e. the relationships and the ability to perform useful searches).

提交回复
热议问题