简体   繁体   English

PHP MYSQL导入Excel序列化数据

[英]PHP MYSQL import excel serialized data

I have a column with serialized data in mysql table. 我在mysql表中有一列带有序列化数据的列。

How to import excell file where I have column like colors where are stored serilized data for checboxes in mysql 如何导入excell文件,其中我有像颜色一样存储在mysql中的checboxes的序列化数据的列

and need to import from excel file column 并需要从excel文件列导入

colors colors colors white yellow blue -> serialized into 1 column in mysql 颜色颜色颜色白色黄色蓝色->在mysql中序列化为1列

structure of excell file can be different. excell文件的结构可以不同。

thanks 谢谢

mysql table mysql表
id | id | name | 名称| colors 颜色
1 | 1 | house | 房子 serialized(yelow,blue...) 序列化(黄色,蓝色...)

excell file excell文件
name | 名称| color | 颜色| color 颜色
house| 房子| yellow | 黄色| blue 蓝色

I'm not sure if is the right way to handle excell file maybe something like this: 我不确定处理excell文件的正确方法是否可能是这样的:

name | 名称| color 颜色
house| 房子| (yellow;blue) (黄色;蓝色)

尝试使用Spreadsheet_Excel_Writer: 链接文本

Convert the spreadsheet to CSV. 将电子表格转换为CSV。 You can read the file contents with fgetcsv(), and save it to the db serialized. 您可以使用fgetcsv()读取文件内容,并将其保存到序列化的db中。

Some code: 一些代码:

$dbh = new PDO($connectionstring, $username, $password);
$handle = fopen('foo.csv', 'r');
fgetcsv($handle); // omit the first line
while($row = fgetcsv($handle)) {
  $name = array_shift($row);
  $stmt = $dbh
    ->prepare('INSERT INTO table(name, data) VALUES(:name, :data);');
  $stmt->bindParam(':name', $name, PDO::PARAM_STR);
  $stmt->bindParam(':data', serialize($row), PDO::PARAM_STR);
  $stmt->execute();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM