Masayuki Tonoki Masayuki Tonoki - 1 month ago 9
MySQL Question

Putting an array into a table with SimpleXMLobject using php

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
);
}

Answer

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')";
   );
  }
Comments