php - 将 xml 提要导入 MySQL 数据库时替换特殊字符

标签 php mysql xml unicode character-encoding

我有一个将 xml 文件的内容导入 MySQL 数据库的 php 脚本,但是字符 é 在数据库中保存为 é

解决此问题的最佳方法是什么,以便字符 é 在数据库中保存为 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>

最佳答案

做一个

htmlspecialchars($variable);

在变量上,可以有一个特殊字符。

关于php - 将 xml 提要导入 MySQL 数据库时替换特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12299284/

相关文章:

php - 如何在不使用 PHP 中的 Adaptive API 或 App ID 的情况下检查给定电子邮件是否为有效的 Paypal 电子邮件 ID

mysql - 聊天服务器数据模型与删除mysql中的每条消息功能

MySQL 如果存在则选择,但如果不存在则显示 no 值

java - 使用 JAXB 解码时的前向兼容性

html - Mozilla 将 <br></br> 翻译成 <br></br><br></br>

c# - 在 Unity 配置中,如何将 connectionString 传递给构造函数?

php - 使用视频ID从PHP API获得YouTube视频质量

php - html 表单提交调用 php 但不重定向

php - 对 array_values 的误解

mysql - 查询在适用时不使用覆盖索引