我有一个大的xml文件,我正在使用XML READER收集特定信息。一旦我将所有信息存储在数组$ content中,然后将其插入到另一个数组$ preOwnedData中。使它成为一个多维数组。
我现在正尝试使用PHP将数组数据插入到已存在的SQL数据库中,但我目前无处可去!我知道使用MYSQLI插入的标准方法。
我假设对于数组,你必须进行某种循环才能通过数组。
数据库已经设置好,我基本上需要在其中插入数组中的字符串。示例:
INSERT INTO $ table(id)VALUES(DocumentID)等等我需要的所有值。
这是ARRAY的var_dump的一部分
array (size=69)
0 =>
array (size=6)
'DocumentID' => string '898052' (length=6)
'ChargeAmount' => string '31500' (length=5)
'URI' => string 'http://imt.boatwizard.com/images/1/80/52/898052_0_040820101331_4.jpg' (length=68)
'MakeString' => string 'Cranchi' (length=7)
'ModelYear' => string '2001' (length=4)
'Model' => string 'Perla 25' (length=8)
1 =>
array (size=6)
'DocumentID' => string '898052' (length=6)
'ChargeAmount' => string '31500' (length=5)
'URI' => string 'http://imt.boatwizard.com/images/1/80/52/898052_0_040820101331_4.jpg' (length=68)
'MakeString' => string 'VOLVO' (length=5)
'ModelYear' => string '2001' (length=4)
'Model' => string '4.3 L' (length=5)
PHP代码更正此代码现在正在运作
$url="https://services.boatwizard.com/bridge/events/38acaac6-c5c6-4aa6-a40c-41fbc1296515/boats?status=on";
$xml = new XMLReader();
$xml->open($url);
$PreOwnedData = array();
while($xml->read()){
//id
if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'DocumentID'){
$content = array();
$xml->read();
$content['DocumentID'] = $xml->value;
}
//price
if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'ChargeAmount'){
$xml->read();
$content['ChargeAmount'] = $xml->value;
}
//img
if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'URI'){
$xml->read();
$content['URI'] = $xml->value;
}
//make
if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'MakeString'){
$xml->read();
$content['MakeString'] = $xml->value;
}
//year
if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'ModelYear'){
$xml->read();
$content['ModelYear'] = $xml->value;
}
//model
if($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'Model'){
$xml->read();
$content['Model'] = $xml->value;
$PreOwnedData[] = $content;
}
}
//Paramaters for SQL connections LOCAL
$hostname="localhost";
$username="root";
$password="";
$database="test";
$table="preowned";
$conn = mysqli_connect($hostname,$username,$password,$database);
//Check connection
if (!$conn) {
die("Connection failed:".mysqli_connect_error());
}
if(count($PreOwnedData) > 0){
foreach($PreOwnedData as $content){
$id = $content['DocumentID'];
$sql = "INSERT INTO $table (id) VALUES ('$id')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
答案 0 :(得分:1)
$table
。定义它。$content['DocumentID'] = $id;
。交换它。