How can i insert into scraped data into mysql?
-
20-12-2019 - |
Question
//Here is the full script. I need to send the returned array results to mysql. I have tried many variations on mySQL insert functions but mysql will still not populate with the data. The table in mysql has the column names 'date','home','score','away'. I am using xamp as a localhost, with database name:'brazil', table name: 'seriea'. Any ideas would be appreciated! thanks.
<?php
$data = array();
$html = file_get_contents('http://www.soccerstats.com/round_details.asp?league=brazil'); //get the html returned from the following url
$doc = new DOMDocument();
libxml_use_internal_errors(true);
if(!empty($html)){
$doc->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$html);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
$entries = $xpath->query('//table[@class="stat"]');
foreach($entries as $key => $value) {
$data[] = array(
'date' => trim($value->getElementsByTagName('font')->item(0)->nodeValue),
'home' => trim($value->getElementsByTagName('font')->item(1)->nodeValue),
'score' => trim($value->getElementsByTagName('font')->item(2)->nodeValue),
'away' => trim($value->getElementsByTagName('font')->item(3)->nodeValue),
);
}
}
echo "<pre>";
print_r($data);
echo "</pre>";
// Create connection
$con=mysqli_connect("***","***","***");
mysqli_select_db('brazil', $con);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$date = $_POST['date'];
$home = $_POST['home'];
$score = $_POST['score'];
$away = $_POST['away'];
$sql="INSERT INTO seriea (date, home, score, away) VALUES ('$date','$home','$score','$away')";
if (!mysqli_query($sql,$con))
{
die('Error: ' . mysqli_error());
}
else{
echo "1 record added";
}
}
mysqli_close($con);
?>
//Output to go to mysql//
Array
(
[0] => Array
(
[date] => 1 Jun 14
[home] => Â FluminenseÂ
[score] => 1 - 1
[away] => Â InternacionalÂ
)
[1] => Array
(
[date] => 1 Jun 14
[home] =>  VitóriaÂ
[score] => 0 - 1
[away] => Â Sport RecifeÂ
)
[2] => Array
(
[date] => 1 Jun 14
[home] => Â CorinthiansÂ
[score] => 1 - 1
[away] => Â BotafogoÂ
)
[3] => Array
(
[date] => 1 Jun 14
[home] => Â ChapecoenseÂ
[score] => 2 - 1
[away] => Â BahiaÂ
)
[4] => Array
(
[date] => 1 Jun 14
[home] => Â CruzeiroÂ
[score] => 3 - 0
[away] => Â FlamengoÂ
)
[5] => Array
(
[date] => 1 Jun 14
[home] => Â SantosÂ
[score] => 2 - 0
[away] =>  CriciúmaÂ
)
[6] => Array
(
[date] => 1 Jun 14
[home] =>  GrêmioÂ
[score] => 0 - 0
[away] => Â PalmeirasÂ
)
[7] => Array
(
[date] => 1 Jun 14
[home] => Â FigueirenseÂ
[score] => 1 - 3
[away] =>  Atlético PRÂ
)
[8] => Array
(
[date] => 31 May 14
[home] =>  São PauloÂ
[score] => 2 - 1
[away] =>  Atlético MGÂ
)
[9] => Array
(
[date] => 31 May 14
[home] => Â CoritibaÂ
[score] => 3 - 0
[away] =>  GoiásÂ
)
[10] => Array
(
[date] => 30 May 14
[home] => Â BahiaÂ
[score] => 0 - 2
[away] => Â SantosÂ
)
[11] => Array
(
[date] => 29 May 14
[home] => Â InternacionalÂ
[score] => 2 - 0
[away] => Â ChapecoenseÂ
)
[12] => Array
(
[date] => 29 May 14
[home] => Â FlamengoÂ
[score] => 1 - 1
[away] => Â FigueirenseÂ
)
[13] => Array
(
[date] => 29 May 14
[home] =>  Atlético MGÂ
[score] => 2 - 0
[away] => Â FluminenseÂ
)
[14] => Array
(
[date] => 29 May 14
[home] =>  Atlético PRÂ
[score] => 2 - 2
[away] =>  São PauloÂ
)
[15] => Array
(
[date] => 29 May 14
[home] => Â CorinthiansÂ
[score] => 1 - 0
[away] => Â CruzeiroÂ
)
[16] => Array
(
[date] => 29 May 14
[home] =>  GoiásÂ
[score] => 0 - 0
[away] =>  VitóriaÂ
)
)
Solution
From the $data
which the data resides, you need to build the query from that. Consider this example:
$values = '';
$initial_statement = 'INSERT INTO `seriea` (`date`, `home`, `score`, `away`) VALUES ';
foreach($data as $key => $value) {
$values[] = "('".implode("','", $value)."')";
}
$values = implode(',', $values);
$complete_statement = $initial_statement . $values;
$query = mysqli_query($con ,$complete_statement);
$complete_statement
should yield something like this:
INSERT INTO `seriea` (`date`, `home`, `score`, `away`) VALUES ('1 Jun 14',' Fluminense ','1 - 1',' Internacional '),('1 Jun 14',' Vitória ','0 - 1',' Sport Recife '),('1 Jun 14',' Corinthians ','1 - 1',' Botafogo '),('1 Jun 14',' Chapecoense ','2 - 1',' Bahia '),('1 Jun 14',' Cruzeiro ','3 - 0',' Flamengo '),('1 Jun 14',' Santos ','2 - 0',' Criciúma '),('1 Jun 14',' Grêmio ','0 - 0',' Palmeiras '),('1 Jun 14',' Figueirense ','1 - 3',' Atlético PR '),('31 May 14',' São Paulo ','2 - 1',' Atlético MG '),('31 May 14',' Coritiba ','3 - 0',' Goiás '),('30 May 14',' Bahia ','0 - 2',' Santos '),('29 May 14',' Internacional ','2 - 0',' Chapecoense '),('29 May 14',' Flamengo ','1 - 1',' Figueirense '),('29 May 14',' Atlético MG ','2 - 0',' Fluminense '),('29 May 14',' Atlético PR ','2 - 2',' São Paulo '),('29 May 14',' Corinthians ','1 - 0',' Cruzeiro '),('29 May 14',' Goiás ','0 - 0',' Vitória ')
Important Note: Please add this on top of your php file
header('Content-Type: text/html; charset=utf-8');
So that you'll get "Fluminense" instead of "Â FluminenseÂ"