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 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?