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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退