如何将数据数组保存到单个mysql字段中?
在mysql表中查询该数组后,如何将其恢复为数组形式?
使用序列化(serialize)和反序列化(unserialize)是否是解决方案?
没有一种好的方法可以将数组存储到单个字段中。
您需要检查关系数据并对模式进行适当的更改。有关此方法的参考,请参见下面的示例。
如果您必须将数组保存到单个字段中,则 serialize()
和 unserialize()
函数可以解决问题。但是您不能对实际内容执行查询。
作为串行化函数的替代方案,还有 json_encode()
和 json_decode()
。
考虑以下数组
$a = array(
1 => array(
'a' => 1,
'b' => 2,
'c' => 3
),
2 => array(
'a' => 1,
'b' => 2,
'c' => 3
),
);
为了将其保存在数据库中,您需要创建一个类似于此的表格
$c = mysql_connect($server, $username, $password);
mysql_select_db('test');
$r = mysql_query(
'DROP TABLE IF EXISTS test');
$r = mysql_query(
'CREATE TABLE test (
id INTEGER UNSIGNED NOT NULL,
a INTEGER UNSIGNED NOT NULL,
b INTEGER UNSIGNED NOT NULL,
c INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (id)
)');
要处理记录,你可以执行以下查询(注意,这只是一个示例,请小心!)
function getTest() {
$ret = array();
$c = connect();
$query = 'SELECT * FROM test';
$r = mysql_query($query,$c);
while ($o = mysql_fetch_array($r,MYSQL_ASSOC)) {
$ret[array_shift($o)] = $o;
}
mysql_close($c);
return $ret;
}
function putTest($t) {
$c = connect();
foreach ($t as $k => $v) {
$query = "INSERT INTO test (id,".
implode(',',array_keys($v)).
") VALUES ($k,".
implode(',',$v).
")";
$r = mysql_query($query,$c);
}
mysql_close($c);
}
putTest($a);
$b = getTest();
connect()
函数返回一个mysql连接资源
function connect() {
$c = mysql_connect($server, $username, $password);
mysql_select_db('test');
return $c;
}
一般情况下,序列化和反序列化是行之有效的方法。
不过如果你的数据比较简单,那么将其保存为逗号分隔的字符串可能更好,因为这样可以节省存储空间。例如,如果你知道你的数组只是一个数字列表,那么你应该使用implode/explode函数。这就是 1,2,3
和 a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}
之间的区别。
如果不是这种情况,那么序列化和反序列化都适用于所有情况。
只需使用 PHP 的 serialize 函数:
<?php
$myArray = array('1', '2');
$seralizedArray = serialize($myArray);
?>
但是,如果您使用的是像这样的简单数组,那么最好使用 implode 和 explode。请改用空白数组而不是 new。
序列化/反序列化数组以便在数据库中存储。
访问http://php.net/manual/en/function.serialize.php
来自PHP手册:
请在页面上查找“返回”
返回包含可以存储在任何地方的值的字节流表示的字符串。
请注意,这是一个二进制字符串,其中可能包含空字节,并且需要作为这样进行存储和处理。 例如,serialize()输出通常应存储在数据库中的BLOB字段中,而不是CHAR或TEXT字段中。
注意:如果要将HTML存储到Blob中,请确保对其进行base64编码,否则它可能会破坏序列化函数。
示例编码:
$YourSerializedData = base64_encode(serialize($theHTML));
$YourSerializedData
现在已经准备好存储到 blob 中。
从 blob 获取数据后,您需要进行 base64_decode 解码,然后反序列化。 解码示例:
$theHTML = unserialize(base64_decode($YourSerializedData));
我发现最好的方法是将数组保存为带有分隔符字符的数据字符串。
$array = array("value1", "value2", "value3", "...", "valuen");
$array_data = implode("array_separator", $array);
$query = "INSERT INTO my_tbl_name (id, array_data) VALUES(NULL,'" . $array_data . "');";
您可以使用简单的查询搜索存储在数组中的数据
$query = "SELECT * FROM my_tbl_name WHERE array_data LIKE '%value3%'";
$array = explode("array_separator", $array_data);
$number = (int)$_POST['number'];
$checked = (boolean) $_POST['checked'];
$name = mysql_real_escape_string($db_pt, $_POST['name']);
$email = mysqli_obj->real_escape_string($_POST['email']);
序列化和反序列化在IT技术中非常常见。您还可以使用JSON,通过json_encode和json_decode来使用一种更少与PHP相关的格式。
如前所述 - 如果您不需要在数组中搜索数据,则可以使用序列化 - 但这仅适用于"php"。因此,我建议使用json_decode / json_encode,不仅能提高性能,也更易于阅读和可移植性(其他语言例如javascript可以处理json_encoded数据)。
嗯,我不知道为什么大家都建议序列化数组。
我认为,最好的方法是将其适配到您的数据库模式中。我不知道(您也没有给出任何线索)数组中实际语义含义,但通常有两种存储这样序列的方法。
create table mydata (
id int not null auto_increment primary key,
field1 int not null,
field2 int not null,
...
fieldN int not null
)
create table mydata (
id int not null auto_increment primary key,
...
)
create table myotherdata (
id int not null auto_increment primary key,
mydata_id int not null,
sequence int not null,
data int not null
)
请查看implode函数,由于值在数组中,您想将数组的值放入mysql查询中,将值插入表中。
$query = "INSERT INto hardware (specifications) VALUES (".implode(",",$specifications).")";
$query = "INSERT INto hardware (specifications) VALUES ("'.implode("','",$specifications)."')";
mysql_query($conn,$query);
另外,如果您不想要重复的值,请将“INto”切换为“IGNORE”,只有唯一的值才会插入到表中。
更新
警告 此扩展在PHP 5.5.0中已弃用,并在PHP 7.0.0中删除。应使用MySQLi或PDO_MySQL扩展。另请参见MySQL:选择API指南。此函数的替代方法包括: