Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Kodowanie znaków prze exportcie bazy MySQL do pliku
Forum PHP.pl > Forum > Przedszkole
krzesik
Witam,
mam poniższą klasę do zrzutu bazy MySQL do pliku, wszystko ładnie działa, ale mam problem z polskimi ogonkami "ą", "ź", "ś" - wpisuje mi krzaki, proszę o podpowiedź

  1. .....
  2. function createDump() {
  3. // Set line feed
  4. $lf = "\n";
  5.  
  6. $resource = mysql_connect($this->getHost(), $this->getDBuser(), $this->getDBpassword());
  7. mysql_select_db($this->getDbname(), $resource);
  8. $result = mysql_query("SHOW TABLES");
  9. $tables = $this->result2Array(0, $result);
  10. foreach ($tables as $tblval) {
  11. $result = mysql_query("SHOW CREATE TABLE `$tblval`");
  12. $createtable[$tblval] = $this->result2Array(1, $result);
  13. }
  14. // Set header
  15. $output = "#". $lf;
  16. $output .= "# DUmp" . $lf;
  17. $output .= "# Version 2.6" . $lf;
  18. $output .= "# ". $lf;
  19. $output .= "# Host: " . $this->getHost() . $lf;
  20. $output .= "# Czas wygenerowania: " . date("M j, Y at H:i") . $lf;
  21. $output .= "# Wersja serwera: ". mysql_get_server_info() . $lf;
  22. $output .= "# Wersja php: " . phpversion() . $lf;
  23. $output .= "# Baza danych : `" . $this->getDBname() . "`" . $lf;
  24. $output .= "#";
  25.  
  26. // Generate dumptext for the tables.
  27. foreach ($tables as $tblval)
  28. {
  29. If (($tblval!="bip_logowanie") and ($tblval!="bip_users"))
  30. {
  31. $output .= $lf . $lf . "# --$tabela------------------------------------------------------" . $lf . $lf;
  32. $output .= "#". $lf . "# Struktura tabeli dla `$tblval`" . $lf;
  33. $output .= "#" . $lf . $lf;
  34. // Generate DROP TABLE statement when client wants it to.
  35. if($this->isDroptables())
  36. {
  37. $output .= "DROP TABLE IF EXISTS `$tblval`;" . $lf;
  38. }
  39. $output .= $createtable[$tblval][0].";" . $lf;
  40. $output .= $lf;
  41. $output .= "#". $lf . "# Zrzut danych tabeli `$tblval`". $lf . "#" . $lf;
  42. $result = mysql_query("SELECT * FROM `$tblval`");
  43. $rows = $this->loadObjectList("", $result);
  44. foreach($rows as $row)
  45. {
  46.  
  47. $insertdump = $lf;
  48. $insertdump .= "INSERT INTO `$tblval` VALUES (";
  49. $arr = $this->object2Array($row);
  50. foreach($arr as $key => $value)
  51. {
  52. $value = addslashes($value);
  53. //$value = str_replace("n", 'rn', $value);
  54. //$value = str_replace("r", '', $value);
  55.  
  56. $insertdump .= "'$value',";
  57. }
  58. $output .= rtrim($insertdump,',') . ");";
  59. }
  60. }
  61. }
  62. mysql_close($resource);
  63. return $output;
  64. }
  65.  
  66. // Private function object2Array.
  67. function object2Array($obj) {
  68. $array = null;
  69. if(is_object($obj)) {
  70. $array = array();
  71. foreach (get_object_vars($obj) as $key => $value) {
  72. if(is_object($value))
  73. $array[$key] = $this->object2Array($value);
  74. else
  75. $array[$key] = $value;
  76. }
  77. }
  78. return $array;
  79. }
  80.  
  81. // Private function loadObjectList.
  82. function loadObjectList($key='', $resource) {
  83. $array = array();
  84. while ($row = mysql_fetch_object($resource)) {
  85. if ($key)
  86. $array[$row->$key] = $row;
  87. else
  88. $array[] = $row;
  89. }
  90. mysql_free_result($resource);
  91. return $array;
  92. }
  93.  
  94. // Private function result2Array.
  95. function result2Array($numinarray = 0, $resource) {
  96. $array = array();
  97. while ($row = mysql_fetch_row($resource)) {
  98. $array[] = $row[$numinarray];
  99. }
  100. mysql_free_result($resource);
  101. return $array;
  102. }
  103. }
  104. .....
Wazniak96
Przypuszczam, iż bazę masz ustawione na UTF-8, a plik na ANSI.
krzesik
Tak, baza jest w UTF-8. Jak wymusić aby zapis był poprawny?
Turson
W notepad++: menu->format->utf8 bez bom
krzesik
Chyba źle to napisałem. Chodzi mi o to aby w pliku wynikowym dane zapisane były w prawidłowej formie. Nie chcę później w żaden sposób zmieniać pliku z kopią bazy.
Damonsson
Pliki tekstowe, mają to do siebie, że mają jakieś kodowanie.

Więc albo je wymuś przy zapisie, albo zmień ręcznie jak napisano wyżej.
Wazniak96
Użyj funkcji iconv

EDIT: Mój błąd. Już poprawione wink.gif
krzesik
już kombinuje jak koń pod góre, ale nic mi z tego nie wychodzi.
do zapisywania danych w pliku mam taki kodzik:

  1.  
  2. $dumper = new Mysqldumper($host, $user, $pswd, $nazwa);
  3. $dumper->setDroptables(true);
  4. $dumpstring = $dumper->createDump();
  5. $plik = "kopie_sql/$nazwa_pliku.sql";
  6. $fp = fopen("$plik", "w");
  7. fwrite($fp, $dumpstring);
  8. fclose($fp);


iconv wstawiałem po otwarciu pliku
$dumpstring = 'iconv("ISO-8859-2","UTF-8","$fp")';
ale też nic nie pomogło, poproszę jakieś wskazówki...

p.s.
a tak na marginesie to jak mam sprawdzić w jakim formacie wypluwa mi skrypt generowany plik? bo może wcale to nie jest UTF-8 (choć tak mam w bazie)
Turson
Tu nie chodzi o kodowanie bazy, ale kodowanie pliku.
Podałem w poście #4 jak kodować.
krzesik
OK, pobrałem, zainstalowałem, zrobiłem zgodnie ze wskazówkami, niestety nic nie pomogło ą jest dalej "±"; , ź jest "Ľ" , ś jest "¶";
jeszcze jakieś pomysły? Nadmieniam, że reszta czcionek jest ok.
com
kiedy wy się nauczycie szukać http://forum.php.pl/index.php?showtopic=74128 smile.gif
krzesik
Dziękuję za wskazówkę, poczyniłem zgodnie z zapisem ale nadal nic z tego mi nie wychodzi. Zamieszczam cały kod jaki mam:
kopia_mysql.php
  1. <?php
  2.  
  3. $nazwa_pliku = date ("Y-m-d H-i-s");
  4.  
  5. include ("mysqldumper.class.php");
  6.  
  7. $dumper = new Mysqldumper($host, $user, $pswd, $nazwa);
  8.  
  9. $dumper->setDroptables(true);
  10. $dumpstring = $dumper->createDump();
  11. $plik = "kopie_sql/$nazwa_pliku.sql";
  12. $fp = fopen("$plik", "w");
  13. fwrite($fp, utf8_encode($dumpstring));
  14. fclose($fp);
  15.  
  16. ?>


mysqldumper.class.php
  1. <?php
  2. /**
  3. * MySQLdumper is a small php class that lets you generate a dump of a MySQL database
  4. * with just 2 lines of code. The dump can be used as a database backup. The dump
  5. * is a valid MySQL-query in plain text. It doesn't depend on the 'mysqldump' command
  6. * line utility, so you won't encounter a problem if this isn't available on the server.
  7. *
  8. * Example 1: Create a database drump.
  9. * <code>
  10. * $dumper = new MysqlDumper($host, $user, $pswd, $nazwa);
  11. * $dumpstring = $dumper->createDump();
  12. * </code>
  13. *
  14. * Example 2: Create a database drump with a 'DROP TABLE IF EXISTS'-statement for each table.
  15. * <code>
  16. * $dumper = new MysqlDumper($host, $user, $pswd, $nazwa);
  17. * $dumper->setDroptables(true);
  18. * $dumpstring = $dumper->createDump();
  19. * </code>
  20. *
  21. * Example 3: Create dumps of different databases.
  22. * <code>
  23. * $dumper = new MysqlDumper("localhost", "user", "password", "database1");
  24. * $dumpstring1 = $dumper->createDump();
  25. * $dumper->setDBname"("database2");
  26. * $dumpstring2 = $dumper->createDump();
  27. * </code>
  28. *
  29. * @package MySQLdumper
  30. * @version 1.0
  31. * @author Dennis Mozes <opensource@mosix.nl>
  32. * @url <a href="http://www.mosix.nl/mysqldumper" target="_blank">http://www.mosix.nl/mysqldumper</a>
  33. * @since php 4.0
  34. * @copyright Dennis Mozes
  35. * @license GNU/LGPL License: <a href="http://www.gnu.org/copyleft/lgpl.html" target="_blank">http://www.gnu.org/copyleft/lgpl.html</a>
  36. **/
  37.  
  38. class Mysqldumper {
  39. var $_host;
  40. var $_dbuser;
  41. var $_dbpassword;
  42. var $_dbname;
  43. var $_isDroptables;
  44.  
  45.  
  46. function Mysqldumper($host = '$host', $dbuser = '$user', $dbpassword = '$pswd', $dbname = '$nazwa') {
  47. $this->setHost($host);
  48. $this->setDBuser($dbuser);
  49. $this->setDBpassword($dbpassword);
  50. $this->setDBname($dbname);
  51. // Don't drop tables by default.
  52. $this->setDroptables(false);
  53. }
  54.  
  55. function setHost($host) {
  56. $this->_host = $host;
  57. }
  58.  
  59. function getHost() {
  60. return $this->_host;
  61. }
  62.  
  63. function setDBname($dbname) {
  64. $this->_dbname = $dbname;
  65. }
  66.  
  67. function getDBname() {
  68. return $this->_dbname;
  69. }
  70.  
  71. function getDBuser() {
  72. return $this->_dbuser;
  73. }
  74.  
  75. function setDBpassword($dbpassword) {
  76. $this->_dbpassword = $dbpassword;
  77. }
  78.  
  79. function getDBpassword() {
  80. return $this->_dbpassword;
  81. }
  82.  
  83. function setDBuser($dbuser) {
  84. $this->_dbuser = $dbuser;
  85. }
  86.  
  87. // If set to true, it will generate 'DROP TABLE IF EXISTS'-statements for each table.
  88. function setDroptables($state) {
  89. $this->_isDroptables = $state;
  90. }
  91.  
  92. function isDroptables() {
  93. return $this->_isDroptables;
  94. }
  95.  
  96. function createDump() {
  97. // Set line feed
  98. $lf = "\n";
  99.  
  100. $resource = mysql_connect($this->getHost(), $this->getDBuser(), $this->getDBpassword());
  101. mysql_select_db($this->getDbname(), $resource);
  102. $result = mysql_query("SHOW TABLES");
  103. $tables = $this->result2Array(0, $result);
  104. foreach ($tables as $tblval) {
  105. $result = mysql_query("SHOW CREATE TABLE `$tblval`");
  106. $createtable[$tblval] = $this->result2Array(1, $result);
  107. }
  108. // Set header
  109. $output = "#". $lf;
  110. $output .= "# " . $lf;
  111. $output .= "# Version 2.6" . $lf;
  112. $output .= "# ". $lf;
  113. $output .= "# Host: " . $this->getHost() . $lf;
  114. $output .= "# Czas wygenerowania: " . date("M j, Y at H:i") . $lf;
  115. $output .= "# Wersja serwera: ". mysql_get_server_info() . $lf;
  116. $output .= "# Wersja php: " . phpversion() . $lf;
  117. $output .= "# Baza danych : `" . $this->getDBname() . "`" . $lf;
  118. $output .= "#";
  119.  
  120. // Generate dumptext for the tables.
  121. foreach ($tables as $tblval)
  122. {
  123. If (($tblval!="bip_logowanie") and ($tblval!="bip_users"))
  124. {
  125. $output .= $lf . $lf . "# --$tabela------------------------------------------------------" . $lf . $lf;
  126. $output .= "#". $lf . "# Struktura tabeli dla `$tblval`" . $lf;
  127. $output .= "#" . $lf . $lf;
  128. // Generate DROP TABLE statement when client wants it to.
  129. if($this->isDroptables())
  130. {
  131. $output .= "DROP TABLE IF EXISTS `$tblval`;" . $lf;
  132. }
  133. $output .= $createtable[$tblval][0].";" . $lf;
  134. $output .= $lf;
  135. $output .= "#". $lf . "# Zrzut danych tabeli `$tblval`". $lf . "#" . $lf;
  136.  
  137. $result1 = mysql_query("SELECT * FROM `$tblval`");
  138. $rows = $this->loadObjectList("", $result1);
  139. foreach($rows as $row)
  140. {
  141.  
  142. $insertdump = $lf;
  143. $insertdump .= "INSERT INTO `$tblval` VALUES (";
  144. $arr = $this->object2Array($row);
  145. foreach($arr as $key => $value)
  146. {
  147. $value = addslashes($value);
  148. //$value = str_replace("n", 'rn', $value);
  149. //$value = str_replace("r", '', $value);
  150. $insertdump .= "'$value',";
  151. }
  152. $output .= rtrim($insertdump,',') . ");";
  153. }
  154. }
  155. }
  156. mysql_close($resource);
  157. return $output;
  158. }
  159.  
  160. // Private function object2Array.
  161. function object2Array($obj) {
  162. $array = null;
  163. if(is_object($obj)) {
  164. $array = array();
  165. foreach (get_object_vars($obj) as $key => $value) {
  166. if(is_object($value))
  167. $array[$key] = $this->object2Array($value);
  168. else
  169. $array[$key] = $value;
  170. }
  171. }
  172. return $array;
  173. }
  174.  
  175. // Private function loadObjectList.
  176. function loadObjectList($key='', $resource) {
  177. $array = array();
  178. while ($row = mysql_fetch_object($resource)) {
  179. if ($key)
  180. $array[$row->$key] = $row;
  181. else
  182. $array[] = $row;
  183. }
  184. mysql_free_result($resource);
  185. return $array;
  186. }
  187.  
  188. // Private function result2Array.
  189. function result2Array($numinarray = 0, $resource) {
  190. $array = array();
  191. while ($row = mysql_fetch_row($resource)) {
  192. $array[] = $row[$numinarray];
  193. }
  194. mysql_free_result($resource);
  195. return $array;
  196. }
  197. }
  198. ?>
  199.  
wlodek_789
1.
  1. <?php
  2. //1. polaczenie z baza danych
  3. //polaczenie z baza danych + ustawienie kodowania na utf8
  4. $dbhost = "localhost";
  5. $dbuser = "root";
  6. $dbpass = "";
  7. $dbname = "base";
  8. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  9.  
  10. //sprawdzenie polaczenia
  11. if (mysqli_connect_errno()) {
  12. printf("Connect failed: %s\n", mysqli_connect_error());
  13. exit();
  14. }
  15.  
  16. //zmaiana znako na utf8
  17. if (!mysqli_set_charset($connection, "utf8")) {
  18. printf("Error loading character set utf8: %s\n", mysqli_error($connection));
  19. } else {
  20. printf("Kodowanie ustawione na: %s\n", mysqli_character_set_name($connection));
  21. }
  22. ?>



2. obiektowo
  1. $objDb = new PDO('mysql:dbname=base;host=localhost', 'root', '', array(
  2. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
  3. PDO::ATTR_PERSISTENT => true
  4. ));



i jeszcze jedno w bazie kodowanie ustawione na utf utf8_general_ci

pliki *.php na utf 8

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body>
  9. </body>
  10. </html>
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.