php 在mysql数据库中保存数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10686333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:48:50  来源:igfitidea点击:

Save array in mysql database

phpmysqlarrays

提问by user1343454

I want to save extra information before sending the total order to Paypal. For each item I have created a single column in my MySQL database where I want to store it. Now I was thinking to save it as an array which I can read later for creating a PHP page. Extra fields are taken from input form fields.

我想在将总订单发送到 Paypal 之前保存额外信息。对于每个项目,我都在我的 MySQL 数据库中创建了一个列,我想在其中存储它。现在我想将它保存为一个数组,稍后我可以读取它来创建一个 PHP 页面。额外的字段取自输入表单字段。

By using an array can I be sure not to mixup information?

通过使用数组,我可以确保不会混淆信息吗?

回答by Emil Vikstr?m

You can store the array using serialize/unserialize. With that solution they cannot easily be used from other programming languages, so you may consider using json_encode/json_decodeinstead (which gives you a widely supported format). Avoidusing implode/explodefor this since you'll probably end up with bugs or security flaws.

您可以使用serialize/存储数组unserialize。使用该解决方案,它们不能轻易地从其他编程语言中使用,因此您可以考虑使用json_encode/json_decode代替(它为您提供广泛支持的格式)。避免为此使用implode/ explode,因为您最终可能会遇到错误或安全漏洞。

Note that this makes your table non-normalized, which may be a bad idea since you cannot easily query the data. Therefore consider this carefully before going forward. May you need to query the data for statistics or otherwise? Are there other reasons to normalize the data?

请注意,这会使您的表未规范化,这可能是一个坏主意,因为您无法轻松查询数据。因此,在继续之前仔细考虑这一点。您是否需要查询数据以进行统计或其他方式?是否有其他原因来规范化数据?

Also, don't save the raw $_POSTarray. Someone can easily make their own web form and post data to your site, thereby sending a really large form which takes up lots of space. Save those fields you want and make sure to validate the data before saving it (so you won't get invalid values).

另外,不要保存原始$_POST数组。有人可以轻松制作自己的 Web 表单并将数据发布到您的站点,从而发送占用大量空间的非常大的表单。保存您想要的那些字段并确保在保存之前验证数据(这样您就不会得到无效值)。

回答by Sliq

The way things like that are done is with serializingthe array, which means "making a string out of it". To better understand this, have a look on this:

完成这样的事情的方式是序列化数组,这意味着“从中制作一个字符串”。为了更好地理解这一点,看看这个:

$array = array("my", "litte", "array", 2);

$serialized_array = serialize($array); 
$unserialized_array = unserialize($serialized_array); 

var_dump($serialized_array); // gives back a string, perfectly for db saving!
var_dump($unserialized_array); // gives back the array again

回答by Amaresh Tiwari

To convert any array (or any object) into a string using PHP, call the serialize():

要使用 PHP 将任何数组(或任何对象)转换为字符串,请调用 serialize():

$array = array( 1, 2, 3 );
$string = serialize( $array );
echo $string;

$string will now hold a string version of the array. The output of the above code is as follows:

$string 现在将保存数组的字符串版本。上述代码的输出如下:

a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}

To convert back from the string to the array, use unserialize():

要将字符串转换回数组,请使用 unserialize():

// $array will contain ( 1, 2, 3 )
$array = unserialize( $string );

回答by Paul

Use the PHP function serialize()to convert arrays to strings. These strings can easily be stored in MySQL database. Using unserialize()they can be converted to arrays again if needed.

使用 PHP 函数serialize()将数组转换为字符串。这些字符串可以很容易地存储在 MySQL 数据库中。unserialize()如果需要,使用它们可以再次转换为数组。

回答by Yasssine ELALAOUI

<?php
$myArray = new array('1', '2');
$seralizedArray = serialize($myArray);
?>

回答by Siva

Store it in multi valued column with a comma separator in an RDBMs table.

将其存储在 RDBMs 表中带有逗号分隔符的多值列中。