1
Array
(
    [0] => Array
        (
            [Title] => SimpleXMLElement Object
                (
                    [0] => aa
                )

            [Pubdate] => SimpleXMLElement Object
                (
                    [0] => aa
                )

            [Link] => SimpleXMLElement Object
                (
                    [0] => aa
                )

        )

    [1] => Array
        (
            [Title] => SimpleXMLElement Object
                (
                    [0] => bb
                )

            [Pubdate] => SimpleXMLElement Object
                (
                    [0] => bb
                )

            [Link] => SimpleXMLElement Object
                (
                    [0] => bb
                )

        )

I want to put this into a table which has (Title, Pubdate, Link) as its columns. I am really confused to how to put it into mysql table when there is SimpleXMLElement Object and [0] in the way. If those were not in the way, I would easily be able to put it into the table, but because those are there and I have never seen them before, I am terribly confused.

This is what I have tried:

foreach($string as $item){
INSERT into table (Title, Pubdate, Link)VALUES($item->title, $item->pubDate, $item->link)
}

FYI this is how I made the array:

$string = $con->channel->item;
$table = array();
foreach ($string as $item) {
    $table[] = array(
        'Title' => $item->title,
        'Pubdate' => $item->pubDate,
        'Link' => $item->link
    );
}
1

3 Answers 3

1

You can't just insert that in the database. Implode first the array.

Try this:

  $string = $con->channel->item;
  $table = array();
   foreach ($string as $item) {
    $sql = mysqli_query("INSERT INTO table (Title, Pubdate, Link)VALUES('$item->title', '$item->pubDate', '$item->link')";
   );
  }
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, but it says Warning: implode() [function.implode]: Invalid arguments passed in
When I do echo $title, there seems to be nothing inside the variable
It says expecting ')' for the line with the second foreach. So I thought that you meant $key -> $val, but then it says cannot access empty property
Sorry I fixed something. Inside the foreach loop
It just comes up with Array, Array, Array, Array, Array, Array, Array, Array if I print_r one of them
|
1

why isnt this working? what happens when you try it?

foreach($string as $item){
INSERT into table (Title, Pubdate, Link)VALUES($item->title, $item->pubDate, $item->link)
}

4 Comments

Parse error: syntax error, unexpected T_STRING so I put it into $sql and did mysql_query($sql, $db_con);
obviously, it has to be inside mysql_query otherwise it wont work, unless that was a mistake. but does that query work now?
What do you mean by that? I'm still a beginner
in the foreach statement above you have the insert query just like that alone without mysql_query. php isnt going to know what to do with that and sees it as a string. so when you added it into mysql_query that tells php that this is a query. thats what i meant.
1

Try something like this..

// output array, to store in database
$result = array();

foreach($table as $key => $simpleXml) {
   $result[$key] = $simpleXml->asXML();
}

// gets your result as a string => you can now insert it into mysql
$dbInsertion = serialize($result);

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.