我有一个 php 脚本,它将 xml 文件的内容导入 MySQL 数据库,但字符é被保存在数据库中为é
解决此问题的最佳方法是什么,以便字符é在数据库中保存为e。
php文件:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'xmltosqldb';
mysql_select_db($dbname);
$sxe = new SimpleXMLElement('http://api.xmlscores.com/matches/?f=xml&c[]=eng_pl&e=1&s=0&l=128&open=e7df44075754027024f0c9e328a64fc4', NULL, TRUE);
foreach($sxe->children() as $match) {
foreach($match->children() as $item) {
echo "{$item->teams->hosts->name}<br />";
$matchstart = $item['timestamp-starts'];
$hostid = $item->teams->hosts[id];
$hostname = $item->teams->hosts->name;
$hostfullname = $item->teams->hosts->fullname;
$awayid = $item->teams->guests[id];
$awayname = $item->teams->guests->name;
$awayfullname = $item->teams->guests->fullname;
$title = $item->details->contest->competition->title;
$season = $item->details->contest->season;
$fixture = $item->details->{'fixture-info'};
$currenttime = date( "Y-m-d H:m:i");
$sql = "SELECT match_status FROM matches WHERE match_id ='$item[id]'";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($result);
//Check if ID exists
//IF YES
if($num == '1'){
$row = mysql_fetch_array($result);
//Check if status changed
if($item[status] != $row['match_status']){
//IF YES
//Change status & Update scores
$sql = "UPDATE matches SET match_status = '$item[status]', match_score = '$item->score', date_updated = '$currenttime' WHERE match_id ='$item[id]'";
echo $sql;
echo '<br />';
echo '<br />';
mysql_query($sql) or die(mysql_error());
if($item[status] == 'finished'){
foreach($item->events->children() as $event) {
$sql = "INSERT INTO events (match_id,match_starts,home_id,away_id,event_type,event_team,event_player,event_score,event_minute,date_updated)
VALUES ('$item[id]',
'$matchstart',
'$hostid',
'$awayid',
'$event[type]',
'$event[team]',
'$event->player',
'$event->score',
'$event->minute',
'$currenttime'
)";
echo $sql;
echo '<br />';
echo '<br />';
mysql_query($sql) or die(mysql_error());
}//END EVENTS FOREACH LOOP
}//IF STATUS == FINISHED
}//IF STATUS CHANGED
}//IF RECORD EXISTS
else{
//IF NO
//Create ID & is Status Finished
$sql = "INSERT INTO matches (
match_contest,
match_id,
match_status,
match_starts,
home_id,
home_name,
home_fullname,
away_id,
away_name,
away_fullname,
match_score,
match_competition_id,
match_competition_title,
match_season,
match_fixture,
date_updated)
VALUES (
'$item[contest]',
'$item[id]',
'$item[status]',
'$matchstart',
'$hostid',
'$hostname',
'$hostfullname',
'$awayid',
'$awayname',
'$awayfullname',
'$item->score',
'$title',
'$title',
'$season',
'$fixture',
'$currenttime')";
mysql_query($sql) or die(mysql_error());
echo $sql;
echo '<br />';
echo '<br />';
if($item[status] == 'finished'){
foreach($item->events->children() as $event) {
$sql = "INSERT INTO events (match_id,match_starts,home_id,away_id,event_type,event_team,event_player,event_score,event_minute,date_updated)
VALUES ('$item[id]',
'$matchstart',
'$hostid',
'$awayid',
'$event[type]',
'$event[team]',
'$event->player',
'$event->score',
'$event->minute',
'$currenttime'
)";
mysql_query($sql) or die(mysql_error());
echo $sql;
echo '<br />';
echo '<br />';
}//END EVENTS FOREACH LOOP
} //END IF STATUS == FINISHED
}//END IF MATCH ID DOESN'T ALREADY EXIST
}//END ITEMS FOREACH LOOP
}//END MATCH FOREACH LOOP
?>
xml 提要:
<?xml version="1.0" encoding="UTF-8"?>
<xslf:livescore-feed xsi:schemaLocation="http://xmlscores.com/XSLF http://xmlscores.com/schemas/2/matches.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xslf="http://xmlscores.com/XSLF" timestamp-created="1346930723">
<matches total-count="1" first="1" last="1">
<item contest="eng_pl" id="8efd06e37b1d9e209f6ec7b0045e9a66" status="finished" timestamp-starts="1346517000">
<teams>
<hosts id="mcfc_en">
<name>Man. City</name>
<fullname>Manchester City FC</fullname>
</hosts>
<guests id="qprfc_en">
<name>QPR</name>
<fullname>Queens Park Rangers FC</fullname>
</guests>
</teams>
<score>3 - 1</score>
<details>
<contest>
<competition id="eng_pl">
<title>eng_pl</title>
</competition>
<season>2012/2013</season>
</contest>
<fixture-info>3</fixture-info>
</details>
<events>
<event xsi:type="xslf:GoalEvent" type="goal" team="hosts">
<player>Touré Y.</player>
<score>1 - 0</score>
<minute>16</minute>
</event>
<event xsi:type="xslf:CardEvent" type="yellow_card" team="hosts">
<player>Kolarov A.</player>
<minute>46</minute>
</event>
<event xsi:type="xslf:GoalEvent" type="goal" team="guests">
<player>Zamora B.</player>
<score>1 - 1</score>
<minute>59</minute>
</event>
<event xsi:type="xslf:GoalEvent" type="goal" team="hosts">
<player>Dzeko E.</player>
<score>2 - 1</score>
<minute>61</minute>
</event>
<event xsi:type="xslf:CardEvent" type="yellow_card" team="hosts">
<player>Rodwell J.</player>
<minute>75</minute>
</event>
<event xsi:type="xslf:CardEvent" type="yellow_card" team="guests">
<player>Zamora B.</player>
<minute>86</minute>
</event>
<event xsi:type="xslf:GoalEvent" type="goal" team="hosts">
<player>Tévez C.</player>
<score>3 - 1</score>
<minute>90</minute>
</event>
</events>
</item>
</matches>
</xslf:livescore-feed>