我已经坚持了一段时间了,我为这个主题所做的所有搜索都没有给我带来解决方案。此代码将XML的第一个产品的值插入MySQL表中。
$xml=simplexml_load_file("URL") or die("Error: Cannot create object");
foreach ($xml->programs->products->product as $product);
foreach ($xml->programs->products->product->product_info->price as $price);
$insert = 'INSERT INTO BBB (price) VALUES (?)';
$insertStmt = $dbconnection->prepare($insert);
foreach ($product as $row) {
$insertStmt->bind_param('s', $price);
}
$insertStmt->execute();
echo "New records created successfully";
在此之后,我得到了反馈,即使用准备好的PHP语句,foreach不能以这种方式工作。此后我创建了这段代码:
$xml=simplexml_load_file("URL") or die("Error: Cannot create object");
$insert = 'INSERT INTO BBB (price) VALUES (?)';
$insertStmt = $dbconnection->prepare($insert);
foreach ($tags as $key=>$val) {
if ($key == "product") {
$dcprice = $key->product_info->price;
$productranges = $val;
for ($i=0; $i < count($productranges); $i+=2) {
$offset = $productranges[$i] + 1;
$len = $productranges[$i + 1] - $offset;
$price[] = $dcprice(array_slice($values, $offset, $len));
}
}
$insertStmt->bind_param('s', $price);
$insertStmt->execute();
}
echo "New records created successfully";
代码回应了新记录的创建,但当我在phpMyAdmin中检查表时,没有任何内容出现。
我一直在检查的链接示例包括:How does PHP 'foreach' actually work?,foreach,xml_parse_into_struct。但是我找不到合适的信息,如果有人可以指出我做错了什么就会很棒。
答案 0 :(得分:0)
以下未经过测试,但我认为应该提出一个想法。创建prepared statement
并将变量绑定到语句,然后在声明变量后执行循环。
$xml=simplexml_load_file("URL") or die("Error: Cannot create object");
$sql = 'INSERT INTO BBB (price) VALUES (?)';
$stmt = $dbconnection->prepare( $sql );
$stmt->bind_param( 's', $price );
foreach ($tags as $key=>$val) {
if ($key == "product") {
$dcprice = $key->product_info->price;
$productranges = $val;
for($i=0; $i < count($productranges); $i+=2) {
$offset = $productranges[$i] + 1;
$len = $productranges[$i + 1] - $offset;
$price = $dcprice(array_slice($values, $offset, $len));
$stmt->execute();
}
}
}
echo "New records created successfully";
插入多条记录的基本示例。
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'xxx';
$dbname = 'experiments';
$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$sql='insert into `temp` (`val`) values (?)';
$stmt=$db->prepare( $sql );
if( $stmt ){
$stmt->bind_param( 's', $i );
for( $i=0; $i < 100; $i++ )$stmt->execute();
$stmt->free_result();
$stmt->close();
}
使用xml文件中的实际数据(但不使用simpleXML
)
$url='https://daisycon.io/datafeed/?filter_id=17940&settings_id=2510&demo';
$dom=new DOMDocument;
$dom->load( $url );
$xp=new DOMXPath( $dom );
$col=$dom->getElementsByTagName('product_info');
if( !empty( $col ) ){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'xxx';
$dbname = 'experiments';
$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$sql='insert into `temp` (`brand`,`title`,`price`) values (?,?,?);';
$stmt=$db->prepare( $sql );
if( $stmt ){
$stmt->bind_param( 'sss', $brand, $title, $price );
foreach( $col as $i => $product ){
$brand = $xp->query( 'brand', $product )->item( 0 )->nodeValue;
$price = floatval( $xp->query( 'price', $product )->item( 0 )->nodeValue );
$title = $xp->query( 'title', $product )->item( 0 )->nodeValue;
$stmt->execute();
}
$stmt->free_result();
$stmt->close();
}
$db=null;
}
这会运行xml并按照预期插入记录 - 我希望这会有所帮助。
读取XML并显示结果的示例 - 无数据库,只是为了显示如何读取记录!
$url='https://daisycon.io/datafeed/?filter_id=17940&settings_id=2510&demo';
$dom=new DOMDocument;
$dom->load( $url );
$xp=new DOMXPath( $dom );
$col=$dom->getElementsByTagName('product_info');
if( !empty( $col ) ){
foreach( $col as $i => $product ){
$data=array(
'brand' => $xp->query( 'brand', $product )->item( 0 )->nodeValue,
'title' => $xp->query( 'title', $product )->item( 0 )->nodeValue,
'price' => floatval( $xp->query( 'price', $product )->item( 0 )->nodeValue )
);
echo '<ul><li>',implode('</li><li>',$data),'</li></ul>';
}
}
然后输出:
<ul>
<li>Dr. Fix</li>
<li>Dr. Fix Organic Droge Voeten Balsem 75 ml</li>
<li>11.95</li>
</ul>
<ul>
<li>Lamberts</li>
<li>Lamberts Selenium ACE 100 TAB</li>
<li>19.1</li>
</ul>
<ul>
<li>Annemarie Borlind</li>
<li>Annemarie Borlind Lipcontourstift Red 1.05 G</li>
<li>13.25</li>
</ul>
<ul>
<li>Wapiti</li>
<li>Wapiti Darmfunctie 20 dragees</li>
<li>3.95</li>
</ul>
<ul>
<li>Davitamon</li>
<li>Davitamon Compleet Mama 60 capsules</li>
<li>11.95</li>
</ul>
<ul>
<li>Vitaminhealth</li>
<li>Vitaminhealth Multi Vitaminen 30 tabletten</li>
<li>9.95</li>
</ul>
<ul>
<li>Vitaminhealth</li>
<li>Vitaminhealth Multi Vitaminen 120 tabletten</li>
<li>32.5</li>
</ul>
<ul>
<li>Vitaminhealth</li>
<li>Vitaminhealth Multi Vitaminen 240 tabletten</li>
<li>58.95</li>
</ul>
<ul>
<li>Vitaminhealth</li>
<li>Vitaminhealth Botformule 60 tabletten</li>
<li>12.95</li>
</ul>
<ul>
<li>Vitaminhealth</li>
<li>Vitaminhealth Betacaroteen 60 softgels</li>
<li>16.95</li>
</ul>
<ul>
<li>Brita</li>
<li>Brita Optimax Cool Wit Waterfilterkan 1 exemplaar</li>
<li>49.95</li>
</ul>
<ul>
<li>Essential Organics</li>
<li>Essential Organics Cal Mag & Zink 90 tabletten</li>
<li>11.5</li>
</ul>
<ul>
<li>Vitotaal</li>
<li>Vitotaal Canadese Geelwortel 45 vegicaps</li>
<li>9.95</li>
</ul>
<ul>
<li>Annemarie Borlind</li>
<li>Annemarie Borlind Combination Skin Dagfluid 75 ml</li>
<li>27.95</li>
</ul>
<ul>
<li>Vitotaal</li>
<li>Vitotaal Cranberry + C 45 vegicaps</li>
<li>9.95</li>
</ul>
<ul>
<li>Vital Cell Life</li>
<li>Vital Cell Life Vitamine B6/B12 foliumzuur 60 cap</li>
<li>12</li>
</ul>
<ul>
<li>Brita</li>
<li>Brita Marella XL Wit Waterfilterkan (3,5 liter) 1 exemplaar</li>
<li>21.5</li>
</ul>
<ul>
<li>Plantina</li>
<li>Plantina Astaxanthin-eco 60 capsules</li>
<li>33.1</li>
</ul>
<ul>
<li>Terrasana</li>
<li>Terrasana Umeboshi 150 G</li>
<li>7.49</li>
</ul>
<ul>
<li>Solgar Vitamins</li>
<li>Solgar Vitamins Digestive Enzymes 250 tabletten</li>
<li>39.9</li>
</ul>