在 MySQL DB 中存储关联数组
我想将主要包含字符串和整数的关联数组存储为数据库中的值。
我在想:
- 内爆/爆炸 - 需要找到分隔符,它不会出现在值中 - 因为它几乎是用户生成的,不安全
- XML - 对于作业(创建/读取值)
- json - 如果我只需要使用
json_decode
/json_encode
,看起来很完美
你觉得怎么样?
请不要向我转发类似的其他问题,我已经阅读了其中的大部分问题,但我仍然不确定:)
I'd like to store associative array containg mostly strings and integers as values in db.
I was thinking:
- implode/explode - need to find delimiter, which won't be in values - since it's almost user generated, not safe
- XML - feels to heavy for the job (creating/reading values)
- json - if i need only to work with
json_decode
/json_encode
, seems perfect
What do you think?
Please, do not forward me to other questions like this on SO, I've read most of them and I'm still not sure :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为序列化:
http://php.net/manual/en/function.serialize.php
I think serialize:
http://php.net/manual/en/function.serialize.php
您可以在写入数据库时使用
serialize()
数组,或json_encode()
数组,以及在从数据库获取数据时使用json_decode()
数组。You could either
serialize()
the array, orjson_encode()
it when writing to the database, andjson_decode()
when fetching from the database.这实际上完全取决于您在数据库中获取该数据后想要检索该数据的方式。当然,您可以序列化一个数组并将其放入您的字段中,但是如果您想使用该数组中的数据执行查询该怎么办?您必须取出序列化数组,执行 PHP 函数,然后执行查询。
您需要解释您想要使用此关联数组数据实现什么目的。
It really all depends on the way in which you want to retrieve this data once you DO get it in the database. Sure you could serialize an array and put it in your field, but what if you want to perform queries with the data in that array; you'd have to pull the serialized array out, perform PHP functions, and then do your query.
You need to explain what it is you want to achieve with this associative array data.
还可以选择创建一个包含
key
和value
列的表,并将每个数组元素单独存储在该表中。它可能看起来有些过分,但根据您想要对数据执行的操作,可能会被证明是有用的There's also the option of creating a table with
key
andvalue
columns, and storing each array element individually in that table. It might seem excessive, but could prove useful depending on what you want to do with the data