开发者

How to store a multi-dimensional javascript array in a mysql database

I have a javascript application that's will keep track of the x&y coordinates of several objects on a 'canvas.' I'm planning on storing this data in a 2-dimensional array such as:

new Array(
    new Array(id0, x0, y0),
    new Array(id1, x1, y1),
    new Array(id2, x2, y2));

I want to submit this data to a php script and store it in a mysql database for later retrieval/redisplay. What's a good way to do this? Should I serialize the arra开发者_StackOverflow社区y and encode it for transport? I'm using jQuery so if anyone knows of any good functions included in this framework that would be great. Thanks!


Serialize your array to a storable format (usually string), send it to the server that will store it to the DB.

You might use JSON, XML or any other format. I'd go for JSON as it's less verbose, then using less space in your database.

However be aware that serializing arrays prevent any querying using the database engine. You will have to extract your data, unserialize it, check if it's the one you need, and rinse and repeat until you found the right array (unless you know the ID of the array you're looking for).

NB: Don't forget to use POST request to send it to the server, as GET request have a length limit.


The best idea seems to be JSON.

Just encode your array in JSON, send it to the server. If you do not need to manipulate data on the server side, you can even save the JSON itself to MySQL. Otherwise, all wide-spread server-side languages have JSON handling functions (json_decode() and friends in PHP).

On the route back (from server to Javascript), JSON again is the simplest way. You can use jQuery's getJSON() or parseJSON() functions to handle it, and get your array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜