1

im pulling in data from an rss feed, i want to add the title and description into the same row (different fields) in a mysql database

$result = $xml->xpath('//title');  //finding the title tags in the xml document and loading into variable
$result1 = $xml->xpath('//description');  //finding the title tags in the xml document and loading into variable

I'm loading the information into variables but don't understand how i can insert this into a mysql table called data?

It sounds easy but im struggling as its an array.

1
  • What is the structure of the RSS file you are reading in? Are you using SimpleXML? Commented Apr 14, 2012 at 2:04

3 Answers 3

2

Try something along these lines : (UPDATED)

<?php
    $con = mysql_connect("localhost","yourdbusername","yourdbpassword");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("yourdbname", $con);

    $query = sprintf("INSERT INTO `data` (`title`, `description`) VALUES ('%s','%s');",
                         mysql_real_escape_string($result),
                         mysql_real_escape_string($result1));

    mysql_query($query);

    mysql_close($con);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

@Amadan I updated my post, although you REALLY got me wondering as to WHY the answer was downvoted; honestly, all I was trying was not to confuse the OP even more (as he simply IS quite confused on the matter, already...)
Because with PHP as insecure as it is, noone should be touching SQL without the clear understanding of the Bobby Tables thing. Teaching someone how to do MySQL and not even mentioning SQL injection is a Bad Thing (tm).
1

You will need to change the node names but this should do it -

<?php
$db = new PDO('mysql:dbname=test;host=127.0.0.1', 'user', 'pass');

$sql = "INSERT INTO tbl (title, description) VALUES (?, ?)";
$stmt = $db->prepare($sql);

$rss = simplexml_load_file('path_to_rss_feed');

foreach ($rss->articles as $article) {
    $stmt->execute(array($article->title, $article->description));
}

Comments

0

Assuming your table is set up with columns that are named "title" and "description" have you tried something like this?

$rows=array();

foreach($result as $title){
  $rows[]="('".mysql_real_escape_string($title)."','".mysql_real_escape_string(array_shift($result1))."')");
}
mysql_query("INSERT INTO data (title, description) VALUES ".implode(',',$rows);

EDIT: fair enough;) added mysql_real_escape_string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.