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 
        )

)
Was it helpful?

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Â"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top