Good day everyone.

Recently I wrote a database backup script for my server. It did not work for some time due to server settings, but now it worked, but another problem appeared. Help solve it.

The problem is that backup is created with broken coding. All Russian characters are unreadable. Tell me what to add to the code to fix this problem?

Here is the code:

$dblocation = "localhost"; $dbuser = "admin"; $dbpasswd = "12345"; $dbcnx = @mysql_connect($dblocation,$dbuser,$dbpasswd); $user_mysql = 'admin'; $pass_mysql = '12345'; $tmp_path = $_SERVER['DOCUMENT_ROOT'].'/uploads/damp/'; function get_database_tables() { $ret = array(); $r = mysql_query('SHOW DATABASES'); if (mysql_num_rows($r)>0) { while($row = mysql_fetch_array($r, MYSQL_NUM)) { $ret[] = $row[0]; } } return $ret; } $h=array(); $h=get_database_tables(); $vsego=count($h); $exс=array('information_schema', 'mysql', 'performance_schema', 'phpmyadmin'); for ($x=0; $x < $vsego; $x++){ $bd=$h[$x]; #И сделаем их дамп if(!in_array($bd, $exс)){ exec('mysqldump -u '.$user_mysql.' -p'.$pass_mysql.' '.$bd.' > ' . $tmp_path . $bd . '.sql'); } } echo "<pre>"; print_r($h); echo "</pre>"; 
  • >> All Russian characters are not readable Where are they unreadable? You open the file in some editor and can not read the Russian text? Or do you import the resulting file and your code returns nonsense (although everything is fine on the main data)? Or, after importing, do you make a selection through the mysql client and there is nonsense instead of Russian text? - BOPOH
  • I open the file in the editor padpad ++ Backups made via PhpMyadmin in the same editor open perfectly - alexsis20102

1 answer 1

 exec('mysqldump -u '.$user_mysql.' -p'.$pass_mysql.' '.$bd.' > ' . $tmp_path . $bd . '.sql'); 

Replaced by

 exec('mysqldump -u '.$user_mysql.' -p'.$pass_mysql.' '.$bd.' --default-character-set=utf8 > ' . $tmp_path . $bd . '.sql'); 
  • The dump is done via mysqldump , but it is not connected with the set names )) - BOPOH
  • @BOPOH, exactly :) I'll fix it right now) - dlarchikov
  • As far as I understand this solution will be for databases encoded with UTF-8 And for encoding Windows-1251 ?? And is there a method to make the definition automatically and save in the desired encoding ?? - alexsis20102
  • @ alexsis20102, most likely you are struggling with windmills. Import your dump - the data will most likely be inserted normally. And with n ++, the problem is in the encoding, try changing it - it should help (you can try to copy the text before changing the encoding, and paste the copied text after the change). I have never had problems when exporting a dump with localization. However, no additional flags indicated. But I did not look at the dump in npp, but most often it is not required. - BOPOH
  • No, there is clearly a problem with the encoding. Everything about what you write, I tried. But knowing AGAVY servers is not surprising. While looking for ways to resolve the issue on his side. - alexsis20102