Chinaunix

标题: 求教高手:数据库插入有重复记录? [打印本页]

作者: tao_627    时间: 2012-05-26 14:24
标题: 求教高手:数据库插入有重复记录?
哪位大侠帮忙看下,下面的php代码在哪里存在问题,为啥插入数据库有重复记录,或者显示的结果不是我所希望的?

//parse xml config file and write items into the specified table in MySQL
   function parseConfigFile($xml_file, $mysql_conn, $tb_name)
   {
       $tbl_count = 0;
       $dom=new DOMDocument();
       $dom->load($xml_file);
       foreach($dom->getElementsByTagname('feed') as $feed)
       {
             $table_name = $feed->getElementsByTagname('table');
         $table_value = $table_name->item(0)->firstChild->nodeValue;
                 echo $table_value.'<br />';
                          
         //first query the specified rss link from feedUrl table if exist?
         $query = 'SELECT tbl_name FROM '.$tb_name.' WHERE tbl_name="'.$table_value.'"';
                 echo $query.'<br />';
         $result = mysql_query($query,$mysql_conn);
                 var_dump($result);
                 echo '<br />';
                 $row = mysql_num_rows($result);
                 echo $row.'<br />';
                 mysql_free_result($result);
                 $query = null;
                 $result = null;
                 var_dump($result);
                 echo '<br />';
         if($row)
         {
           echo 'already exist a table item '.$table_value.' in the current table, count number  '.$tbl_count;
           echo '<br />';
                   continue;
         }
         else
         {
                   echo 'I am coming in';
           //if no exist, insert this record into data table feedUrl
           $insert_query = "INSERT INTO ".$tb_name." (tbl_name,last_update) VALUES ('".$table_value."', NOW())";
                   echo $insert_query.'<br />';
                   $insert_result = mysql_query($insert_query,$mysql_conn);
           
           if($insert_result == 1)
                   {
                      $tbl_count++;
              echo 'write a record ok';
                   }
           else
              echo $insert_result.mysql_error();
           echo '<br />';
         }
         //mysql_free_result($result);
       }
           echo 'already load all tables with the total number '.$tbl_count;
   }
   
作者: tao_627    时间: 2012-05-26 14:26
本帖最后由 tao_627 于 2012-05-26 14:27 编辑

我的意思是解析如下xml文件,将它写入数据库,可是数据库为空时,它显示里面存在记录,而且在大数据量操作时,会插入重复的记录:
<?xml version="1.0" encoding="utf-8"?>
<rss>
<feed>
<table>Current_Event_Copy</table>
</feed>
<feed>
<table>Finance_Economics_Copy</table>
</feed>
<feed>
<table>Social_Humanity_Copy</table>
</feed>
<feed>
<table>Science_Technology_Copy</table>
</feed>
<feed>
<table>Life_Fashion_Copy</table>
</feed>
<feed>
<table>Sports_Car_Copy</table>
</feed>
<feed>
<table>Reading_Travel_Copy</table>
</feed>
<feed>
<table>Entertainment_Game_Copy</table>
</feed>
</rss>
作者: tao_627    时间: 2012-05-26 16:20
已经搞定了,多谢各位!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2