0

I have a working code

<?php

$con    =   mysql_connect('localhost','test','test');
mysql_select_db('test',$con);


require_once("xml2json.php");

$testXmlFile = 'myxml.xml';

$xmlStringContents = file_get_contents($testXmlFile); 
$jsonContents = "";
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
$obj =json_decode($jsonContents);
$rows = array();
foreach($obj->rss->channel->item as $item) {

 $rows[] = "('".mysql_real_escape_string($item->title)."','".mysql_real_escape_string($item->description)."')";

 } 
        $del_horoscope = "delete from jos_horoscope";
        mysql_query($del_horoscope);

        $query = "INSERT INTO `jos_horoscope` (`title`,`description`) VALUES ".implode(', ',$rows);
        print_r ($query);
        mysql_query($query);
  // Do the query - do NOT show the output of mysql_error() in a production environment!
  if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();

?>

But data is inserting twice with every run of this script. I am using delete statement to delete the data first before inserting new one.Can anyone please help me solve this twice problem. Here is teh screenshot of twice Data filled .http://i.imgur.com/7IM1mOE.png?1

NEW EDIT

Here is the INSERT QUERY echo http://pastebin.com/btZ7f85a The data is not duplicate.

Thanks in advance

4

1 回答 1

0

这是未经测试的,但尝试这样的事情。我将删除更改为 TRUNCATE,这将删除表中的所有数据。我还更改了 INSERT 的工作方式,而不是 1 长屁股插入,我将其更改为多个插入。不应该有巨大的性能影响。试试看,告诉我。

<?php

$con = mysql_connect('localhost','test','test');
mysql_select_db('test',$con);

require_once("xml2json.php");

$testXmlFile = 'myxml.xml';

$xmlStringContents = file_get_contents($testXmlFile); 
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
$obj =json_decode($jsonContents);
$rows = array();

foreach($obj->rss->channel->item as $item) 
{
    $rows[] = "('".mysql_real_escape_string($item->title)."','".mysql_real_escape_string($item->description)."')";
} 

// Remove Data in Table.
$truncate = mysql_query("TRUNCATE TABLE jos_horoscope");

foreach($rows as $row)
{
    $query = "INSERT INTO `jos_horoscope` (`title`,`description`) VALUES $row";
    $result = mysql_query($query) or die(mysql_error());
}
?>
于 2013-04-26T02:36:38.173 回答