I have an rest api endpoint that lets you upload a file into the server. When I save a file that is named using the english alphabet, there is no issue. But when I try to save a file that has a japanese character, the file is saved and the filename in the server is okay but when I look into the database the filename is not right.
Here is the result in my DB when saving the file.
But when i look into the server, the file name is correct
I tried changing the database collation into
utf8mb4_unicode_ci
utf8
utf8_general_ci
but the issue still persist.
Update: Here is my insert query to insert the data
public function setFileQuery($param,$file){
$this->module_id = $param['moduleid'];
$this->file_name = $file['name'];
$this->file_size = $file['size'];
$q = "INSERT INTO data_file
(module_id,file_name,file_size,start_time,end_time,elapse_time,through_put)
VALUES
(?,?,?,?,?,?,?)";
$insertStmt = $this->conn->prepare($q);
$insertStmt->execute([
$this->module_id,
$this->file_name,
$this->file_size,
null,
null,
null,
null,
]);
Responses::http_ok();
}
}
PS: For filename that has an spaces, i converted the spaces into _
since there is an error when downloading the file. But this issue is not relevant in converting filename since the filename doesnt have spaces and It works on my local correctly.
CodePudding user response:
Please make sure that both the table collation and the field collation is utf8 (e.g. utf8_general_ci)
(1) First, the table collation should be set utf8 (e.g. utf8_general_ci)
(2) And make sure that the data fields are also utf8 (e.g. utf8_general_ci)
(3) Now if you may insert data (e.g. thru the Phpmyadmin panel or thru any PHP script):
For PHP :
If you are using PDO: Make sure you have set the charset in your connection, such as:
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
If you are using Mysqli:
$conn = mysqli_connect($servername, $username, $password, $dbname);
/* change character set to utf8 */
mysqli_set_charset($conn,"utf8");
(4) The result will be fine: