我有一个大的xml文件,我正在使用xml READER收集特定的信息。一旦我将所有信息存储在数组$content中,它就会被插入到另一个数组$preOwnedData中。使它成为一个多维数组。
我现在试图插入数组数据到一个已经存在的SQL数据库使用PHP,但我目前无处可去!我知道用mysql插入的标准方法。
我假设对于数组,你必须做某种循环来遍历数组。
数据库已经建立,我基本上需要插入数组中的字符串。示例:
INSERT INTO $table (id) VALUES (documententid)等所有我需要的值
这是数组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)
$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;
}
}
}
- 您没有调用实际执行插入的函数。叫它。
- 在你的函数中,
$table
没有定义。定义它。 - 同样,这是倒退的…
$content['DocumentID'] = $id;
。交换。
祝你好运!