将xml feed导入MySQL数据库时替换特殊字符

时间:2012-09-06 11:40:39

标签: php mysql xml unicode character-encoding

我有一个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 Feed:

<?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>

3 个答案:

答案 0 :(得分:0)

做一个

htmlspecialchars($variable);

关于变量,可能有特殊字符。

答案 1 :(得分:0)

马特,你知道mysql_ *函数在新的php版本中是 DEPRECATED 吗?从现在开始,即使是php.net上的官方指南也说要使用PDO或mysqli _ *。

此外,作为开发人员的甜蜜饼干,注射和特殊字符不再有痛苦。尝试一下,你会喜欢它:)

例如某些代码(主要基于您的php脚本):

// This is DataBase connection
try{
    $dbh = new PDO("mysql:host=$host;dbname=$name", $user, $pass);      
}
catch(PDOException $e){
    die ('Error Connecting to DB!');
}

// Example for your first SELECT
$q = $DB->prepare("SELECT match_status FROM matches WHERE match_id = ?"); 
if( $q->execute( array($item[id]) ) ) 
{
    $result = $q->fetch(PDO::FETCH_ASSOC);
    //..other code
}

在$ result变量中的这段代码之后你会得到一个数组。

有关详情:http://www.php.net/manual/en/book.pdo.php

答案 2 :(得分:0)

你确定php文件和数据库表/列的编码都是UTF-8(没有字节顺序标记)吗?如果其中一个不是,那很容易造成这种情况!