【问题标题】:INSERT into XML contents into mysql DB将 XML 内容插入 mysql DB
【发布时间】:2017-08-04 18:41:15
【问题描述】:

基本上我有一个调用外部 XML 文件的 php 脚本

然后它将某些数据从 xml 文件插入到 foreach 循环内的 mysql 数据库中

但是当我检查数据库时它是空的

我做错了什么

xml包含以下结构

<jobs>
    <job>
        <jobref>987654321</jobref>
        <title>TITLE HERE</title>
        <url>
        URL HERE
        </url>
        <salary>&pound;85 - &pound;105/day benifits</salary>
        <location>LOCATION</location>
        <description>desc</description>
        <category>CAT HERE</category>
    </job>
    <job>
        <jobref>123456789</jobref>
        <titleTITLE HERE</title>
        <url>
        URL HERE
        </url>
        <salary>&pound;85 - &pound;105/day benifits</salary>
        <location>LOCATION</location>
        <description>desc</description>
        <category>CAT</category>
    </job>
</jobs>

下面是PHP代码

<?php
    $dbu = 'user';
    $dbp = 'pass';
    $dbh = 'localhost';
    $dbn = 'dbname';

    $dbc = mysql_pconnect($dbh,$dbu,$dbp);

    // Get XML Feed
    $startPage = $_GET['page'];
    $perPage = 3000;
    $currentRecord = 0;
    $xml = simplexml_load_file("urlhere");
    $jobs = $xml->xpath("/jobs/job");

    foreach ($jobs as $job)
    {
        mysql_select_db($dbn,$dbc);
        $query = mysql_query("INSERT INTO `listings` (`jobref`,`title`,`url`,`salary`,`location`,`description`,`category`) VALUES ('$job->jobref','$job->title','$job->salary','$job->location','$job->description','$job->category')",$dbc);

        echo "$job->jobref Added <br>";
    }
?>

它显示:

120239811 Added 
201909016 Added 
202482907 Added 

当我运行脚本时,它会解析 xml 文件,但实际上并没有插入数据

任何帮助都会很棒,因为我的时间非常紧迫 在此先感谢

** 在下面编辑 ** 下面的新代码

<?php
        $startPage = 1;
        $perPage = 3000;
        $currentRecord = 0;
        $xml = simplexml_load_file("urlhere");
        $jobs = $xml->xpath("/jobs/job");
        mysql_select_db($dbn,$dbc);
        foreach ($jobs as $job)
        {
        $currentRecord += 1;
         if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)){ 
        $query = mysql_query("INSERT IGNORE INTO `listings` (jobref,title,url,salary,location,description,category) VALUES ('$job->jobref','$job->title','$job->url','$job->salary','$job->location','$job->description','$job->category')",$dbc);
        if($query){
        echo "$job->jobref Added <br>";
        } else {
        echo mysql_error();
        }
        }
        }
        ?>

现在只是在 XML 文件中添加第一个条目

从查询中删除 IGNORE 并添加 在重复键更新 jobref =$job->jobref+1 还是一样的问题

【问题讨论】:

  • 每次在新代码中使用the mysql_ 数据库扩展a Kitten is strangled somewhere in the world 时,它都已被弃用,并且已经存在多年,并且在 PHP7 中永远消失了。如果您只是学习 PHP,请花精力学习 PDOmysqli 数据库扩展和预处理语句。 Start here
  • 尝试像if (! $query) { echo mysql_error(); } 一样测试$query 然后你可能会看到一些有用的东西
  • 还将mysql_select_db($dbn,$dbc);移到foreach循环之外,只需执行一次
  • upper cmets 是很好的陈述,您应该执行这些建议,还将这两件事放在您的脚本之上 error_reporting(E_ALL);ini_set('display_errors', 1); 这将为您提供有关脚本中发生的错误的更多详细信息
  • 我刚刚修改了脚本并检查了 db & script 中的列数并且都匹配

标签: php mysql xml insert


【解决方案1】:

你确定xml加载成功了吗? :) 但我从您的示例中猜想 xml 节点 &lt;salary&gt; 具有非法 & 字符 "&amp;" here the link about it

但你可以用一点技巧来处理它like this

我建议你使用mysqli,所以根据你的xml示例

...

$conn = mysqli_connect($dbh, $dbu, $dbp);
if(!$conn) die('connect fail: '. mysqli_error($conn));

$db = mysqli_select_db($conn, $dbn);
if(!$db) die('db fail: '. mysqli_error($conn));

... 

$file = file_get_contents(url_here);

// replace '&' to '&amp;'
$file = str_replace('&', '&amp;', $file);
$xml = simplexml_load_string($file);

$jobs = $xml->xpath("/jobs/job");

foreach ($jobs as $job)
{
    ...

    // just get back those ampersand
    $job->salary = str_replace('&amp;', '&', $job->salary);

    $s = "INSERT IGNORE INTO `listings` (`jobref`,`title`,`url`,`salary`,`location`,`description`,`category`) VALUES('$job->jobref','$job->title','$job->url','$job->salary','$job->location','$job->description','$job->category')";

    $q = mysqli_query($conn, $s);

    if(!$q) {
        die(mysqli_error($conn));
    }else {
        echo "$job->jobref Added <br>";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多