如何在mysql数据库中存储多维javascript数组

时间:2021-10-19 02:11:48

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:

我有一个javascript应用程序,它将跟踪“画布”上几个对象的x和y坐标。我打算将这些数据存储在二维数组中,例如:

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 array 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!

我想将这些数据提交到php脚本并将其存储在mysql数据库中以便以后检索/重新显示。这样做的好方法是什么?我应该序列化数组并对其进行编码以便传输吗?我正在使用jQuery,所以如果有人知道这个框架中包含的任何好的功能都会很棒。谢谢!

2 个解决方案

#1


2  

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.

您可以使用JSON,XML或任何其他格式。我会选择JSON,因为它不那么冗长,然后在数据库中使用更少的空间。

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).

但请注意,序列化数组会阻止使用数据库引擎进行任何查询。你必须提取你的数据,反序列化,检查它是否是你需要的,然后冲洗并重复,直到找到正确的数组(除非你知道你正在寻找的数组的ID)。

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

注意:不要忘记使用POST请求将其发送到服务器,因为GET请求有长度限制。

#2


2  

The best idea seems to be JSON.

最好的想法似乎是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).

只需用JSON编码您的数组,将其发送到服务器。如果您不需要在服务器端操作数据,您甚至可以将JSON本身保存到MySQL。否则,所有广泛使用的服务器端语言都有JSON处理函数(json_decode()和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.

在返回的路径上(从服务器到Javascript),JSON再次是最简单的方法。您可以使用jQuery的getJSON()或parseJSON()函数来处理它,并获取您的数组。

#1


2  

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.

您可以使用JSON,XML或任何其他格式。我会选择JSON,因为它不那么冗长,然后在数据库中使用更少的空间。

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).

但请注意,序列化数组会阻止使用数据库引擎进行任何查询。你必须提取你的数据,反序列化,检查它是否是你需要的,然后冲洗并重复,直到找到正确的数组(除非你知道你正在寻找的数组的ID)。

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

注意:不要忘记使用POST请求将其发送到服务器,因为GET请求有长度限制。

#2


2  

The best idea seems to be JSON.

最好的想法似乎是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).

只需用JSON编码您的数组,将其发送到服务器。如果您不需要在服务器端操作数据,您甚至可以将JSON本身保存到MySQL。否则,所有广泛使用的服务器端语言都有JSON处理函数(json_decode()和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.

在返回的路径上(从服务器到Javascript),JSON再次是最简单的方法。您可以使用jQuery的getJSON()或parseJSON()函数来处理它,并获取您的数组。