T&R splet blog o spletnih storitvah. Razgaljamo tehnologijo!

reference spletnih strani
21st August

Konverzija CP1250, ISO-8859-1 baze v UTF-8 – šumniki UTF-8 – cp1250 to utf8

Na T&R Splet smo enkrat že obdelali probleme, ko imamo UTF-8 bazo, vendar v tej bazi shranjene CP1250 podatke. Vzrok tega je, da imamo nastavljeno CP1250 povezavo na bazo, kar pomeni, da vse podatke v bazo shranjujemo v CP1250. Problem nastane, ko stranka/programer želi spletno stran spremeniti v popolni UTF-8 način, kjer nato dobi popačene znake za č,š,ž in druge posebne znake, ki imajo v UTF-8 rezervirane dva byta.

Postopek kako konvertirati strukturo in podatke baze v UTF-8 način:

1.) Naredite backup baze
2.) V root direktorij dodajte skripto z imenimom utf8convert.php, s sledečo kodo po wordpress vodiču:

PHP:
  1. $tables = array();
  2. $tables_with_fields = array();
  3.  
  4. $link_id = mysql_connect('host', 'user', 'password') or die('Error establishing a database connection');
  5. mysql_select_db(DB_NAME, $link_id);
  6.  
  7. // Gathering information about tables and all the text/string fields that can be affected
  8. $resource = mysql_query("SHOW TABLES", $link_id);
  9. while ( $result = mysql_fetch_row($resource) )
  10. $tables[] = $result[0];
  11.  
  12. if ( !empty($tables) ) {
  13.     foreach ( (array) $tables as $table ) {
  14.         $resource = mysql_query("EXPLAIN $table", $link_id);
  15.         while ( $result = mysql_fetch_assoc($resource) ) {
  16.             if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
  17.             $tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL "( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
  18.         }
  19.     }
  20.  
  21.     // Change all text/string fields of the tables to their corresponding binary text/string representations.
  22.     foreach ( (array) $tables as $table )
  23.     mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);
  24.  
  25.     // Change database and tables to UTF-8 Character set.
  26.     mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET utf8", $link_id);
  27.     foreach ( (array) $tables as $table )
  28.     mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);
  29.  
  30.     // Return all binary text/string fields previously changed to their original representations.
  31.     foreach ( (array) $tables_with_fields as $table => $fields ) {
  32.         foreach ( (array) $fields as $field_type => $field_options ) {
  33.             mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
  34.         }
  35.     }
  36.  
  37.     // Optimize tables and finally close the mysql link.
  38.     foreach ( (array) $tables as $table )
  39.     mysql_query("OPTIMIZE TABLE $table", $link_id);
  40.     mysql_close($link_id);
  41. } else {
  42.     die('<strong>There are no tables?</strong>');
  43. }

3.) Zaženite http://vasa_domena.si/utf8convert.php
4.) Sledite navodilom kako vašo spletno stran pre-konvertirati v UTF-8 način

Deli s skupnostjo:

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Podobni članki:

  1. Stalni UTF-8 problemi – utf8 mysql php – čšž šumniki težave – iskanje znaki – mysql like
  2. Šumniki – UTF-8 – Težave – PHP & MySQL
  3. PHP – Konvertiranje datotek CP1250, UTF-8 – iconv
  4. UTF-8 tabele, vendar latin1 podatki ter povezava
  5. PHP – delo z UTF-8 stringi oz. podatki – težave s šumniki (č,š,ž)
  6. Rezanje teksta po koncu besede – php funkcija – substr – cut text – UTF-8
  7. Kako preveriti ali že obstaja določena MySQL tabela
  8. Brisanje testnih naročil v Magento internetni trgovini
  9. MySQL kreiranje tabele s SELECT stavkom
  10. Zend Search Lucene – UTF8 podpora – iconv težave

1 komentar na “Konverzija CP1250, ISO-8859-1 baze v UTF-8 – šumniki UTF-8 – cp1250 to utf8”

  1. Stalni UTF-8 problemi – utf8 mysql php – čšž šumniki težave – iskanje znaki | .: TRSplet - internetne storitve :. je napisal:

    [...] WordPress.org « Konverzija CP1250, ISO-8859-1 baze v UTF-8 – šumniki UTF-8 – cp1250 to utf8 [...]

Dodaj komentar