如何将多个数组导入 mysql 数据库?
how can I import an multiple array into a mysql database?
如何使用 php 将多数组导入 mysql 数据库?
我创建的数据库只有一个 table(工作)和 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
)
)
)
您可以使用 $str = serialize()
保存到数据库中,然后使用 [=11g=] 将其取回。
$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;
是的,我知道这非常低效。
这样的事情会
<?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 class 用法你可以看到 http://php.net/manual/en/book.pdo.php
希望对您有所帮助。
下面的代码是 运行 但只有一个字段我无法导入。
// 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>
如何使用 php 将多数组导入 mysql 数据库?
我创建的数据库只有一个 table(工作)和 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
)
)
)
您可以使用 $str = serialize()
保存到数据库中,然后使用 [=11g=] 将其取回。
$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;
是的,我知道这非常低效。
这样的事情会
<?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 class 用法你可以看到 http://php.net/manual/en/book.pdo.php
希望对您有所帮助。
下面的代码是 运行 但只有一个字段我无法导入。
// 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>