dpj775835868 2017-06-11 10:14
浏览 39
已采纳

SQL - 获取RSS提要并插入SQL

Question: How to Fetch and Insert RSS FEED into MySQL?

I want to fetch and insert data from an rss feed, in this case kotakus rss feed (kotaku.com/vip.xml) .

Currently: i get an error with PDO method: Parse error: syntax error, unexpected '(' in D:\xampp\htdocs\xml\sql_pdo.php on line 11

I'll show Code for three steps. First to Fetch Data that is currently working. Second is a PDO method to fetch&insert data into mySQL ( not working currently ). Third is same as two but using Curl instead (not working either). Im using PhpMyAdmin and Xampp for the MySQL database. I just want either second or third method or another method to work.. only thing that matters is to get results.

First Code:

<?php
$xml= simplexml_load_file("http://kotaku.com/vip.xml"); 
foreach($xml->channel->item as $itm){
    $title = $itm->title;
    $description = $itm->description;
    echo $title;
    echo $description;
} ?>

Second (PDO)

<?php
$db = new PDO('mysql:host=localhost;dbname=simpledata','root','123456');

$xmldoc = new DOMDocument();
$xmldoc = load('http://kotaku.com/vip.xml');

$xmldata = $xmldoc->getElementsByTagName('channel');
$xmlcount = $xmldata->length;

for ($i=0; $i < $xmlcount; $i++) {
    $title = $xmldata->item($i)->getElementsByTagName('title')(0)->childNodes->item(0)->nodeValue;
    $description = $xmldata->item($i)->getElementsByTagName('description')(0)->childNodes->item(0)->nodeValue;

    $stmt = $db->prepare("insert into kotaku values(?,?)");
    $stmt->bindParam(1,$title);
    $stmt->bindParam(1,$description);
    $stmt->execute();
    printf($title);
} ?>

Third (CURL)

<?php

$url="http://kotaku.com/vip.xml"; //need complete
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl-setopt($ch, CURLOPT_URL, $url);  //get the url contents

$data = curl_exec ($ch); //execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);

$con=mysql_connect("localhost","root","123456"); //connect to server 
mysql_select_db("simpledata", $con) or die (mysql_error()); //select database

foreach ($xml -> item as $row) {
    $title = $row -> title;
    $description = $row -> description;

// performing sql query

$sql = "INSERT INTO 'kotaku' ('title', 'description')"
        . "VALUES ('$title', '$description')";

$result = mysql_query($sql);
if (!$result) {
    echo 'MySQL ERROR';
    } else {
    echo 'SUCCES';
    }
?>
  • 写回答

1条回答 默认 最新

  • doubairan4213 2017-06-11 10:27
    关注

    You cant use pdo bindParam without specifying the binding data type. Use bindValue instead:

      $stmt->bindValue(1, $title);
      $stmt->bindValue(2, $description);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?