//这是完整的脚本。 我需要将返回的数组结果发送到mysql。我在mySQL插入函数上尝试了很多变种,但mysql仍然不会填充数据。 mysql中的表格包含列名称' date',' home',' score',#39; away'。我使用xamp作为本地主机,数据库名称为:' brazil',表名:' seriea'。任何想法,将不胜感激!感谢。
<?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);
?>
//输出到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Â
)
)
答案 0 :(得分:0)
从数据所在的$data
开始,您需要从中构建查询。考虑这个例子:
$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
应该产生这样的结果:
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');
这样你就可以得到“Fluminense”而不是“Fluminense”