我将数据从xml导入到SQL表时遇到问题。 xml看起来像这样:
<MeasuringPoints>
<MeasuringPoint ID="BaseStation" LastChange="2013-05-20T12:15:03.837">
</MeasuringPoint>
<MeasuringPoint ID="MiddleStation" LastChange="2012-08-01T11:47:33.160">
</MeasuringPoint>
<MeasuringPoint ID="MountainStation" LastChange="2013-05-20T12:15:03.840>
</MeasuringPoint>
</MeasuringPoints>
使用php(foreach
循环)
foreach($skiarea->MeasuringPoints->MeasuringPoint as $measuring) {
$MEASURING_POINTS = $measuring['ID'];
$MEASURING_LAST_CHANGE = $measuring['LastChange'];
}
我在输出中得到这些数据:
BaseStation - 1369052103
MiddleStation - 1343821653
MountainStation - 1369052103
我正在尝试将该值插入batabase
mysql_query("
INSERT INTO database_name SET
base_station_last_change = '$MEASURING_LAST_CHANGE[0]',
middle_station_last_change = '$MEASURING_LAST_CHANGE[1]',
mountain_station_last_change = '$MEASURING_LAST_CHANGE[2]',
") OR die(mysql_error());
但是这段代码不起作用。只导入一个值。
如何将这些数据插入sql数据库字段?
答案 0 :(得分:0)
$MEASURING_LAST_CHANGE = array();
foreach($skiarea->MeasuringPoints->MeasuringPoint as $measuring) {
$MEASURING_POINTS = $measuring['ID'];
$MEASURING_LAST_CHANGE[] = $measuring['LastChange'];
}
mysql_query("
INSERT INTO table_name (base_station_last_change,middle_station_last_change,mountain_station_last_change) VALUES (
'$MEASURING_LAST_CHANGE[0]','$MEASURING_LAST_CHANGE[1]','$MEASURING_LAST_CHANGE[2]')
") OR die(mysql_error());
答案 1 :(得分:0)
foreach($skiarea->MeasuringPoints->MeasuringPoint as $measuring) {
$MEASURING_POINTS[] = $measuring['ID'];
$MEASURING_LAST_CHANGE[] = $measuring['LastChange'];
}