且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何附加数据库中已经存在的序列化字符串

更新时间:2023-11-25 21:52:34

您必须从表行中读取该列.将其反序列化为PHP变量,然后向其添加新的匹配项.

You have to read the column from your table row. Unserialize it into a PHP variable and then add a new occurance to it.

然后序列化新阵列并将其存储回数据库中

Then serialize the new array and store it back to your database

// SELECT from table
$s = 'a:7:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;i:6;i:7;}';

$d = unserialize($s);

print_r($d);

$d[] = 99;
print_r($d);

$s2 = serialize($d);
echo $s2;

// UPDATE table row

结果

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 99
)
a:8:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;i:6;i:7;i:7;i:99;}

不建议这样存储数据,因为它使该数据无法使用查询进行处理

Storing data like this is not recommended, as it makes this data impossible to process using queries