dounieyan2036 2018-02-15 08:46
浏览 36
已采纳

PHP检查数据库中XML文件的值

I have several entries in XML file with "special number".

I have to search the database for these values.

If the number was not found in the database, then this "special number" and associated data should be stored in the database.

<?php
foreach($array_from_xml_file as $key => $value) {

    if($key === 'special_nummer'){

        $sql = $db->loadArray("SELECT email FROM table WHERE special_nummer = $value");

            // If nothing found insert new Data.

        if(empty($sql)){
            $insert_data = $db->insertData("INSERT INTO table (special_nummer, title, content) VALUES ('$key','$value','....')");
        }
    }
}
?>

Disadvantage of this founction places a heavy load on the database.

My second method

I load the whole column with "special numbers" from database into an XML file or array.

Find the numbers that are not in the database and create a new array. Save this array to database.

My question: How do I make it most efficient?

  • 写回答

1条回答 默认 最新

  • douguaidian8021 2018-02-15 08:56
    关注

    Use INSERT IGNORE:

    foreach($array_from_xml_file as $key => $value) {
        if($key === 'special_nummer'){
            $insert_data = $db->insertData("INSERT IGNORE INTO table (special_nummer, title, content) VALUES ('$key','$value','....')");
        }
    }
    

    special_nummer has to be primary key or unique

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line