这是我的数组:
Array ( [0] => Natural Chlid 1 [1] => Natural Chlid 2 [2] => Natural Chlid 3 )
我如何做以下事项:
a)转换数据,以便可以通过php查询调用并插入db
b)将数据仅添加到数据库的一个FIELD(在这种情况下是一个名为'children'的字段)
答案 0 :(得分:3)
您可能需要serialize()和unserialize():
$data = array(
0 => 'Natural Chlid 1',
1 => 'Natural Chlid 2',
2 => 'Natural Chlid 3'
);
// To save
$link = mysqli_connect('hostname', 'user', 'password', 'dbname');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$serialized = mysqli_real_escape_string($link, serialize($data));
$result = mysqli_query($link, "INSERT INTO table ('id', 'children') VALUES (123, '$serialized')");
if (!$result) {
printf("Error message: %s\n", mysqli_error());
}
// To retrieve
$result = mysqli_query("SELECT * FROM table WHERE id=123");
$row = mysqli_fetch_assoc(result);
$data = unserialize($row['children']);