Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problemy z kodowaniem utf-8
Forum PHP.pl > Forum > PHP
deirathe
Mam ustawione names w sql na utf-8, pliki zapisane w utf-8, i po zapisaniu pliku zml w bazie i odczytaniu go i tworzeniu simplexml dostaje bledy:
JAK WYLACZE SET NAMES
  1. Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 7: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xC4 0x3F 0x3C 0x2F in /frea/assets/core/DataService.class.php on line 15
  2.  
  3. Warning: simplexml_load_string() [function.simplexml-load-string]: <item route="2">Kupi?</item> in /frea/assets/core/DataService.class.php on line 15
  4.  
  5. Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /frea/assets/core/DataService.class.php on line 15

a jak wlacze gina znaki polskie, mam ? zamiast ogonkow:
KOD klasy DB:
  1. <?php
  2. class Db
  3. {
  4.    private $connection;
  5.    public     $db;
  6.    public    $query;
  7.    private    $qString;
  8.    private $qBuilder;
  9.    private $runBuilder = false;
  10.    private $lastResults;
  11.    private $dataContainer;
  12.    public function __construct($host,$user,$pass,$db)
  13.    {
  14.        $this->connection = mysql_connect($host,$user,$pass);
  15.        if(!$this->connection)
  16.            Application::Exception(mysql_errno(),mysql_error(),dirname(__FILE__),"nieznany",$this->qString);
  17.        if(!mysql_select_db($db))
  18.            Application::Exception();
  19.        $this->query("SET NAMES utf8");
  20.    }
  21.    
  22.    public function Query($sql)
  23.    {
  24.        $sql?$this->qString=$sql:$sql;
  25.        if(!$this->query=mysql_query($sql))
  26.        {
  27.            Application::Exception(mysql_errno(),mysql_error(),dirname(__FILE__),"nieznany",$this->qString);
  28.            return false;
  29.        }
  30.        return $this->query;
  31.    }
  32.    private function QueryBuilder($opt,$str)
  33.    {
  34.        $this->qBuilder[$opt] = $str;
  35.    }
  36.    private function BuildQuery()
  37.    {
  38.        if(!$this->qBuilder['FROM'])
  39.        {
  40.            return $this->query;
  41.        }
  42.        if(!$this->qBuilder['SELECT'])
  43.            $this->qBuilder['SELECT'] = "*";
  44.        $query= "SELECT ".$this->qBuilder['SELECT']." FROM ".$this->qBuilder['FROM'];
  45.        
  46.        if($this->qBuilder['WHERE'])
  47.            $query.=" WHERE ".$this->qBuilder['WHERE'];
  48.        if($this->qBuilder['ORDER'])
  49.            $query =" ORDER BY ".$this->qBuilder['ORDER'];
  50.        if($this->qBuilder['LIMIT'])
  51.            $query.=" LIMIT ".$this->qBuilder['LIMIT'];
  52.        $this->qBuilder = "";
  53.        //echo $query;
  54.        return $this->Query($query);
  55.    }
  56.    public function Select()
  57.    {
  58.  
  59.        $columns = func_get_args();
  60.        //print_r($columns);
  61.        if(count($columns))
  62.            $select = "".implode(",",$columns)."";
  63.        else
  64.            $select = "*";
  65.        
  66.  
  67.        $this->QueryBuilder("SELECT",$select);
  68.        
  69.        return $this;
  70.    }
  71.    public function On($case)
  72.    {
  73.        $this->QueryBuilder("ON",$case);
  74.        return $this;
  75.    }
  76.    public function Join($table)
  77.    {
  78.        $this->QueryBuilder("JOIN",$table);
  79.        return $this;
  80.    }
  81.    public function From($table)
  82.    {
  83.        $this->QueryBuilder("FROM",$table);
  84.        return $this;
  85.    }
  86.    public function Where($where)
  87.    {
  88.        $this->QueryBuilder("WHERE",$where);
  89.        return $this;
  90.    }
  91.    public function Order()
  92.    {
  93.        $colums = func_get_args();
  94.        if(count($columns))
  95.            $order = implode(",",$columns);
  96.        else
  97.            $order = "";
  98.        $this->QueryBuilder("ORDER",$order);
  99.        return $this;
  100.        
  101.    }
  102.    public function Limit()
  103.    {
  104.        $limits = func_get_args();
  105.        if(count($limits)>1)
  106.            $limit = $limits[0].",".$limits[1];
  107.        else
  108.            $limit = $limits[0];
  109.        $this->QueryBuilder("LIMIT",$limit);
  110.        return $this;
  111.    }
  112.    public function NumResults($sql=null)
  113.    {
  114.        if(!$sql)
  115.            $this->BuildQuery();
  116.        else    
  117.            $this->Query($sql);
  118.        return mysql_num_rows($this->query);
  119.    }
  120.    public function GetResults($resultsType= A_ARRAY)
  121.    {
  122.        $this->BuildQuery();
  123.        $results = array();
  124.        
  125.        switch($resultsType)
  126.        {
  127.            case A_ARRAY:
  128.                while($temp = @mysql_fetch_assoc($this->query))
  129.                {
  130.                        $results[] = $temp;
  131.                }
  132.            break;
  133.            case M_ARRAY:
  134.                while($temp = @mysql_fetch_array($this->query))
  135.                {
  136.                        $results[] = $temp;
  137.                }
  138.            break;
  139.            case _OBJECT:
  140.                while($temp = @mysql_fetch_object($this->query))
  141.                {
  142.                        $results[] = $temp;
  143.                }
  144.            break;
  145.            default:
  146.                while($temp = @mysql_fetch_assoc($this->query))
  147.                {
  148.                        $results[] = $temp;
  149.                }
  150.            break;
  151.        }    
  152.        if(count($results)==1)
  153.        {
  154.            return $results[0];
  155.        }
  156.        $this->lastResults = $results;
  157.        return $results;
  158.    }
  159.    public function LastResult()
  160.    {
  161.        return $this->lastResults;
  162.    }
  163.    
  164.    public function Delete()
  165.    {
  166.        if(!$this->qBuilder['FROM'])
  167.        {
  168.            $this->lastResults = false;
  169.            return false;    
  170.        }
  171.        $sql = "DELETE FROM ".$this->qBuilder['FROM'];
  172.        if($this->qBuilder['WHERE'])
  173.            $sql.=" WHERE ".$this->qBuilder['WHERE'];
  174.        if($this->qBuilder['LIMIT'])
  175.            $sql.=" LIMIT ".$this->qBuilder['LIMIT'];
  176.        $this->qBuilder= "";
  177.        $this->Query($sql);
  178.        $this->lastResults = mysql_affected_rows();
  179.        return $this->lastResult;
  180.    }
  181.    public function Data($data)
  182.    {
  183.        
  184.        if(is_object($data) and get_class($data)=="stdClass")
  185.        {
  186.            $vars = get_object_vars($data);
  187.            
  188.        }
  189.        elseif(is_array($data))
  190.        {
  191.            $vars = $data;
  192.        }
  193.        //echo "<pre>";
  194.        //print_r($data);
  195.        foreach($vars as $key=>$value)
  196.        {
  197.            if($value=="NOW()")
  198.                $sets.=", `".$key."` = ".$value;
  199.            else
  200.                $sets.=", `".$key."`='".mysql_escape_string($value)."'";
  201.            
  202.        }
  203.        $sets = substr($sets,1);
  204.        $this->dataContainer = $sets;
  205.        return $this;
  206.    }
  207.    public function Escape($str)
  208.    {
  209.        return mysql_escape_string($str);
  210.    }
  211.    public function Update($data="")
  212.    {
  213.        if($data)
  214.            $this->Data($data);
  215.        if(!$this->qBuilder['FROM'] or !$this->dataContainer)
  216.        {
  217.            $this->lastResults = false;
  218.            return false;
  219.        }
  220.        $sql = "UPDATE `".$this->qBuilder['FROM']."` SET ".$this->dataContainer;
  221.        if($this->qBuilder['WHERE'])
  222.            $sql.= " WHERE ".$this->qBuilder['WHERE'];
  223.        $this->Query($sql);
  224.        $this->qBuilder = "";
  225.        $this->lastResults = mysql_affected_rows();
  226.        return $this->lastResults;
  227.    }
  228.    public function Insert($data="")
  229.    {
  230.        if($data)
  231.            $this->Data($data);
  232.        if(!$this->qBuilder['FROM'] or !$this->dataContainer)
  233.        {
  234.            $this->lastResults = false;
  235.            return false;
  236.        }
  237.        $sql = "INSERT INTO ".$this->qBuilder['FROM']." SET ".$this->dataContainer;
  238.        $this->Query($sql);
  239.        $this->qBuilder = "";
  240.        $this->lastResults = mysql_insert_id();
  241.        return $this->lastResults;
  242.    }
  243.    public function GetQuery()
  244.    {
  245.        return $this->qString;
  246.    }
  247.  
  248. }
  249. ?>

kod klasy dataservice:
  1. <?php
  2. class DataService
  3. {
  4.    private $Db;
  5.    public function __construct($db)
  6.    {
  7.        $this->Db = $db;
  8.        
  9.    }
  10.    public function __get($name)
  11.    {
  12.        $this->Db->From("dataService")->Where("name LIKE('".$name."')");
  13.        $r = $this->Db->GetResults();
  14.        
  15.        return simplexml_load_string($r['xmlData']);
  16.    }
  17. }
  18. ?>


Kod pliku ktory zapisuje:
  1. <?php
  2. class Index extends Module
  3. {
  4.    public function __preload()
  5.    {
  6.        
  7.    }
  8.    public function __default()
  9.    {
  10.        
  11.        $this->Db->From("dataService")->Insert(array("xmlData"=>'<?xml version="1.0" encoding="utf-8"?>
  12. <categories>
  13.    <item route="1">
  14.        Nieruchomości
  15.  
  16.        <item route="1">Sprzedam</item>
  17.        <item route="2">Kupię</item>
  18.        <item route="3">Poszukuję do wynajęcia</item>
  19.        <item route="4">Mam do wynajęcia</item>
  20.    </item>
  21.    <item route="2">
  22.        Sprzęt elektroniczny
  23.        <item route="1">Sprzedam</item>
  24.        <item route="2">Kupię</item>
  25.        <item route="3">Zamienię</item>
  26.        <item route="4">Naprawa</item>
  27.    </item>
  28.    <item route="3">
  29.        Motoryzacja
  30.        <item route="1">Sprzedam</item>
  31.        <item route="2">Kupię</item>
  32.        <item route="3">Zamienię</item>
  33.        <item route="4">Części zamienne</item>
  34.    </item>
  35.    <item route="4">
  36.        Usługi
  37.        <item route="1">Poszukuję</item>
  38.        <item route="2">Oferuję</item>
  39.    </item>
  40.    <item route="5">
  41.        Praca
  42.        <item route="1">Poszukuję</item>
  43.        <item route="2">Zatrudnię</item>
  44.    </item>
  45.    <item route="6">Towarzyskie</item>
  46.    <item route="7">
  47.        Nauka
  48.        <item route="1">Korepetycje</item>
  49.        <item route="2">Języki obce</item>
  50.        <item route="3">Podręczniki i inne</item>
  51.    </item>
  52.    <item route="8">
  53.        Zdrowie i uroda
  54.        <item route="1">Usługi</item>
  55.        <item route="2">Inne</item>
  56.    </item>
  57.    <item route="9">
  58.        Różne
  59.        <item route="1">Sprzedam</item>
  60.        <item route="2">Kupię</item>
  61.        <item route="3">Zamienię</item>
  62.        <item route="4">Oddam</item>
  63.        <item route="5">Zwierzęta</item>
  64.        <item route="6">Inne</item>
  65.        <item route="7">Szukam</item>
  66.    </item>
  67. </categories>
  68.        
  69.        ',"name"=>"categories"));
  70.        $this->Template->categories = $this->DataService->categories;
  71.        
  72.        
  73.        $this->Template->ApplyTemplate("tpl");
  74.  
  75.    }
  76.    public function __denied($method)
  77.    {
  78.        
  79.    }
  80.    public function Supervised()
  81.    {
  82.        return false;
  83.    }
  84.    public function __error($e)
  85.    {
  86.        
  87.    }
  88.    public function register()
  89.    {
  90.        echo $_SERVER['REQUEST_URI'];
  91.    }
  92.    
  93. }
  94. ?>
jezoo
a srawdzales z header? w pliku ktory wyswietla wynik daj na poczatku:
  1. <?php
  2. header('Content-Type: text/html; charset=UTF-8');
  3. ?>
deirathe
tak nic nie daje sad.gif
webdice
Użyj edytora który koduje plik w UTF-8.
jezoo
Cytat(deirathe)
pliki zapisane w utf-8


jak by nie patrzac ma zapisane w utf-8 chyba, ze tylko koledze sie wydaje, ze zapisane jest w utf-8 winksmiley.jpg
deirathe
Jest UTF-8 korzystam z eclipsa i zapisuje pliki w utf, set encoding i daje utf-8... problem jest jak zapisze a pozniej odczytam dane z bazy
jeszcze sprawdze pliki dla porownania zapis i odczyt czy bedzie ok

Chyba rozwiazalem ale jednak nie dkonca
jak przestawilem w badzie na columnie metode porownywania napisow na utf8_general_ci to sa polskie znaki , ale ja mam w bazie ponad 100 takich kolumn i co teraz :questionmark.gif? jakies koncepcje?
misiek172
no to jak miało ci działać dobrze kodowanie jak nie miałeś dobrej metody porównywania napisów ;P

teraz ładne po kolei przestawiasz każda kolumnę haha.gif (ew. jakieś zapytanie które wszędzie przestawi ale nie jestem pewien czy takie istnieje winksmiley.jpg)

dla pewności jeszcze przestaw kodowanie całej bazy MySQL, nie tylko tabel
deirathe
Wlasnie to zrobilem ale buga walnalem haha.gif... no nic dzieki za zainteresowanie
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.