I can't save data to database from file_get_html($url) function. My script is showing data nicely by scraping from a url. But I can't save the showed data to a database. It shows error between object and array. I can't even show data by array index. Such as $value[0]. Here is my code sample:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
}
mysqli_close($con);
?>
Try this and let me know if it works:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
}
$value = base64_encode($value);
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
mysqli_close($con);
?>
$value need convert to a string before write to DB. You can show array or object use
echo '<pre>'; print_r($value); echo '</pre>';
or
var_dump($value)