如何使用php将多个数组导入mysql数据库?
我创建的数据库只有一个表(作业)和3个字段(plz,ort,work)
以后更多字段,我想每天运行几次cron作业以使数据库成为实际数据库。该数组来自我想在我的数据库中导入的其他数据库。
我的阵列:
stdClass Object
(
[Code] => 0
[Message] =>
[Data] => Array
(
[0] => Array
(
[plz] => 12345
[ort] => Testort
[work] => Schlosser
)
[1] => Array
(
[plz] => 12345
[ort] => Testort
[work] => Schlosser
)
)
)
答案 0 :(得分:0)
您可以使用$str = serialize()
保存到数据库中,然后使用unserialize($str)
将其恢复。
答案 1 :(得分:0)
$str = '';
foreach($data as $values){
//because we all love foreaches
$str .= "(";
while(list($key, $value) = each($values)){
if($key == 'work'){
$str .= "'$value' ";
} else {
$str .= "'$value', ";
}
}
$str .= ")";
}
$query = "INSERT INTO jobs VALUES" . $str . "";
echo $query;
是的,我知道这是非常低效的。
答案 2 :(得分:0)
这样的事情
<?php
$stdObject = new stdClass();
$stdObject->data = array(
array(
"plz" => "12345",
"ort" => "Testort",
"work" => "Schlosser",
),
array(
"plz" => "12345",
"ort" => "Testort",
"work" => "Schlosser",
)
);
// sql statement to insert data
$sql = "INSERT INTO job (plz,ort,work) VALUES :values";
// empty array to keep each row insertion
$values = array();
// makes following peaces
// ('12345', 'Testort', 'work')
// and push them into $values array.
foreach ($stdObject->data as $job) {
$value .= "('" . $job['plz'] . "', '"
. $job['ort'] . "', '"
. $job['work'] . "')";
$values[] = $value;
}
// convert values array to string
$values_string = implode(', ', $values);
// sql injection safe insertion
$pdo->prepare($sql);
$pdo->execute(array(
'values' => $values_string
));
另外,对于Pdo课程,您可以看到http://php.net/manual/en/book.pdo.php
我希望这会有所帮助。
答案 3 :(得分:0)
以下代码正在运行但只有一个我无法导入的字段。
// and push them into $values array.
foreach ($myarray->Data as $job) {
$value = "('"
. $job['plz'] . "','"
. $job['ort'] . "','"
. $job['work'] . "')";
$values[] = $value;
}
// convert values array to string
$values_string = implode(', ', $values);
// sql statement to insert data
$sql = "INSERT INTO jobs (plz,ort,work) VALUES $values_string";
有更多字段,但其中一个包含这样的HTML代码。用户确实从word进入系统。
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif'><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif'><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif'> </P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><B style="mso-bidi-font-weight: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'>Lorem ipsum dolor<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</SPAN></P>