0

Very confused about charsets.

I've a script to download a .sql file that contains Sql tables. I use MySql. My tables are like this:

CREATE TABLE `xxx` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `notes` mediumtext NOT NULL
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I export those tables with php using this code

$mysqli = new mysqli('localhost', $user, $pass, $name);
$mysqli->select_db($name);
$mysqli->query("SET NAMES 'utf8'");

$content = "";

$queryTables = $mysqli->query('SHOW TABLES');

//the script go on creating $content


header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-disposition: attachment; filename="' . $filename . '"');
echo utf8_decode($content);
exit();

When I open my sql file I found "?????" instead of "èéèàì"

Uploading the file into Plesk cause data error.

If I change

$mysqli->query("SET NAMES 'utf8'");

into

$mysqli->query("SET NAMES 'utf8mb4'");

the file is correct, but Plesk still cause data error.

N.B. Using Notepad++ I can see that, even though the file should be all in utf8, the encoding is ANSI

Which is the correct way to do this?

Gianluca Demarinis
  • 1,964
  • 2
  • 15
  • 21
  • Seems that the problem is the final utf8_decode. I just remove it and seems this worked. Any tips? – Gianluca Demarinis Oct 19 '22 at 13:27
  • See "question mark" (not "black diamond") in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Oct 19 '22 at 15:30

0 Answers0