在单行中将数组插入数据库 [英] Insert array into database in a single row

查看:205
本文介绍了在单行中将数组插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否可行?要在数据库的一个字段中插入数组。

I wonder if this would be doable ? To insert an array into one field in the database.

例如,我有一个标题,我想有一个标题只有一个id,但它将是双语在网站上使用。

For instance I have a title, I want to have that title with only one id, but it's going to be bilingually used on the website.

感觉有点不必要使另一个表具有其全局id,然后另一个表与实际标题链接到表与全局id 。

It feels a bit unnecessary to make another table to have their global ids and then another table with the actual titles linked to the table with the global id.

我只想要这样的东西

ID      TITLE
1       Array("english title", "nederlandse titel");

我使用PHP / MSYQL,所以如果它是可行的,你可以用这些语言解释。

I'm using PHP/MSYQL, so if it would be doable could you please explain in these languages.

哦,我想我可以格式化它很时髦,使用split函数将其重新转换成数组。但我不知道我是否可以立即存储它作为数组马上,我case用户可能键入的东西与格式相同(一百万)

Oh yeah I figured that I could format it funky and use the split function to turn it into an array again. But I wonder if I could just store it as an array right away, I case the user might type something with the same formatting (one out of a million)

推荐答案

它是可行的:

$title = serialize($array);

,然后解码:

$title = unserialize($mysql_data);

但如前所述,它首先真的减轻了数据库的好处。我绝对建议您改用多表格或多栏选项,具体取决于您要支持的语言数量,如果该数字将来会改变。

but as mentioned it really lessens the benefits of a database in the first place. i'd definitely suggest looking into a multi-table or multi-column option instead, depending on the amount of languages you want to support and if that number will change in the future.

编辑 dcousineau (请参阅评论)提及的好处

edit: a good point mentioned by dcousineau (see comments)


有时,序列化输出,即使在转义之后,也会将字符抛入查询中。您可能想要将serialize()包装在base64_encode()调用中,然后在unserialize之前使用base64_decode()。

Sometimes the serialized output, even after escaping, throws characters into the query that screws things up. You may want to wrap your serialize() in base64_encode() calls and then use base64_decode() before you unserialize.

这些情况:

$title = base64_encode(serialize($array) );
$title = unserialize(base64_decode($mysql_data) );

这篇关于在单行中将数组插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆