eevan79 Posted January 22, 2011 Share Posted January 22, 2011 I am using small script (with crone) to backup database every morning, zip and send me mail (with .sql/zip attached). But backup is not in UTF-8 format, so I get this: "Č" -> "C" "Š" -> "S" etc. I have tried various solution but nothing works. Also tried utf_encode() function, but then I get strange characters. Script looks like this: function backup_tables($host,$user,$pass,$name,$tables = '*',$date) { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //ALL TABLES if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } //GET DATA foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $num_fields = mysql_num_fields($result); $return.= 'DROP TABLE IF EXISTS '.$table.';'; $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i < $num_fields; $i++) { while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = str_replace("\n","\\n",$row[$j]); if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");\n"; } } $return.="\n\n\n"; } //SAVE $handle = fopen('Backup-'.$date.'.sql','w+'); fwrite($handle,$return); fclose($handle); } Database Collation: utf8_binType: [/size]MyISAM [/size]How to save .sql file in proper UTF-8 Format?[/size] Link to comment https://forums.phpfreaks.com/topic/225284-backup-database-in-utf-8/ Share on other sites More sharing options...
requinix Posted January 22, 2011 Share Posted January 22, 2011 Quick question: Why not use mysqldump for this? Link to comment https://forums.phpfreaks.com/topic/225284-backup-database-in-utf-8/#findComment-1163454 Share on other sites More sharing options...
eevan79 Posted January 22, 2011 Author Share Posted January 22, 2011 $link = mysql_connect($dbhost,$dbuser,$dbpass); mysql_select_db($dbname,$link); $backupFile = $dbname . date("d-m-Y-H-i-s") . '.gz'; $command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile"; system($command); It creates empty .gzip file. It doesnt work. Link to comment https://forums.phpfreaks.com/topic/225284-backup-database-in-utf-8/#findComment-1163466 Share on other sites More sharing options...
eevan79 Posted January 22, 2011 Author Share Posted January 22, 2011 Fixed $command = "/usr/bin/mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupFile"; Link to comment https://forums.phpfreaks.com/topic/225284-backup-database-in-utf-8/#findComment-1163468 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.