2012-09-06 188 views
2

我有一个PHP脚本,其中进口的XML文件中的内容到MySQL数据库时更换特殊字符,但性格é被保存在数据库作为é导入XML饲料到MySQL数据库

是什么使得字符é被保存在数据库ê解决这个问题的最好办法。

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> 
+0

可能http://stackoverflow.com/questions/132318/how-do-i-correct-the-character-encoding-of-a-file可以回答你的问题。 –

回答

0

htmlspecialchars($variable); 

的变量,这可能有一个特殊字符。

0

马特,你知道mysql_ *的功能是DEPRECATED在新的PHP版本?从现在起甚至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

+0

这是如何回答这个问题? –

+0

准备好的语句将使数据插入正确和准确 – StasGrin

0

你确定这两个你的PHP文件的编码和数据库表/列是UTF-8(无字节顺序标记)?如果其中一个不是,那很可能会造成这种情况!