I have a utf8 json file containing some stuff like that :
"type":"Feature","geometry":{"type":"LineString","coordinates":[[-73.6371999890417,45.451049968774],[-73.6362864025022,45.4507004803266]]},"properties":{"FIN_GCH":191,"TYP_VOIE":"avenue","LIM_GCH":"Montréal-Ouest","NOM_VOIE":"Brock","CLASSE":0,"DEB_DRT":0,"FIN_DRT":0,"LIM_DRT":"Montréal-Ouest","LIE_VOIE":"","DEB_GCH":173,"ID_TRC":1602102,"DIR_VOIE":"Sud","SENS_CIR":-1}}
,{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-73.6382813452807,45.4514760942107],[-73.6371999890417,45.451049968774]]},"properties":{"FIN_GCH":171,"TYP_VOIE":"avenue","LIM_GCH":"Montréal-Ouest","NOM_VOIE":"Brock","CLASSE":0,"DEB_DRT":150,"FIN_DRT":152,"LIM_DRT":"Montréal-Ouest","LIE_VOIE":"","DEB_GCH":143,"ID_TRC":1602103,"DIR_VOIE":"Sud","SENS_CIR":-1}}
With the help of a php script, i put all the data in a mysql UTF8 database but even so, the entries have weird accents : instead of Montréal-Ouest i have Montréal-Ouest
Do you have any idea what is the problem please ?
Thank you
I had to set a utf8 connection with the server by setting the options array like this
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
);
$connection = new PDO( $dns, $user, $password, $options );
Thanks to CBroe
Related
I have a REST API endpoint that lets you upload a file to the server. When I save a file that is named using the latin 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.
The result in my DB when saving the file:
But when I look at the server's shell the filename is correct:
I tried changing the database collation into:
utf8mb4_unicode_ci
utf8
utf8_general_ci
...but the issue still persists.
Update: This is my 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();
}
First, the table collation should be set utf8mb4.
Second, make sure that the data field (aka column) collations are also utf8mb4.
Now you may insert data (e.g. thru phpMyAdmin or thru any PHP script):
phpMyAdmin:
PHP:
If you are using PDO then make sure you have set the charset in your connection, such as:
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass);
If you are using MySQLi:
$conn = mysqli_connect($servername, $username, $password, $dbname);
/* change character set to utf8mb4 */
mysqli_set_charset($conn,"utf8mb4");
$mysqli->query("set names utf8mb4");
...and then executing the query, i.e.:
mysqli_query( "INSERT INTO song( songname ) VALUES( 'あああ.txt' )" );
The result will be as wanted:
We are using PHP and Oracle.
Connection string :
$connect_db = oci_connect($username, $password, $host_db, 'AL32UTF8');
Also in php code.
header('Content-Type: text/html; charset=utf-8');
My Problem is inside the php page, it is converted to regional language. but When it is stored into Oracle db it is stored like '¿¿¿¿¿¿'.
How can we solve this?
Any Oracle function is available equivalent to mysql_query( set names 'utf8' ).
Previously we are using UTF8 instead of "AL32UTF8" in connection string.
We can use this format
$connect_db = oci_connect($username, $password, $host_db, 'AL32UTF8');
I have encountered a problem, when i try to execute a sql statement I have stored in a file. The problem is that special characters like æøå becomes gibberish when they are imported to the database.
I use the code below:
$dsn = 'mysql:host='.DBHOST.';dbname='.DBBASE;
$db = new PDO($dsn, DBUSER, DBPASS);
//$db->exec("set names 'utf8'");
$sql = "TRUNCATE TABLE wtm_meta_language";
$db->exec($sql);
$sql = file_get_contents('language.sql');
$db->exec($sql);
The language.sql file is encoded in UTF-8 and so is the database.
I have tried to force the database connection to use utf-8 by adding "set names 'utf8'" to my code (row 3, that is now a comment), but when I add this row nothing gets imported at all.
I hope someone has an idea to how to solve this.
Your results are not getting retrieved because you did not add charset=utf8 .
$db = new PDO('mysql:host='.DBHOST.';dbname='.DBBASE.';charset=utf8', 'username', 'password');
and then it should allow you to get the required result.
Could be that PHP does not detect that the file is UTF-8 since, according to your comment, the BOM is missing.
I found this little function here which should force it to read UTF-8.
function file_get_contents_utf8($fn) {
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
$result = #file_get_contents($fn,false,$context);
return $result;
}
Also, if I were you I'd use mysql_set_charset to set the character set on the mysql connection. Also I'd use SHOW VARIABLES LIKE 'character_set%'; so see what the current connection character set is.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
Just a simple question.. I am using UTF-8 as standard in both my MySQL database and HTML file.. One thing that I don't understand is why a letter is shown differently in SQL than when I typed it. I am sure it should be placed the exact same way as I write it, when using UTF-8.
Is there a class or something that automatically keeps all posted data in UTF-8 and then posts it as it is?
Your text editor also has an effect on your character encoding. Be sure to use a text editor or IDE that you can set the character encoding to UTF-8.
Try this 1. method if not succes 2.metod. Last method i say open HTML File with notepad2 editor and change encoding from File menu to UTF8 and save. i think it will be solution;
Method
$dsn = $config ["driver"] . ':dbname=' . $config ['dbname'] . ';host=' . $config ['host'];
. ';charset=UTF-8'; //---> this is the solution
try {
$this->link = new PDO ( $dsn, $config ['user'], $config ['pass'] );
// The mysql driver ignores the charset directive in $dsn (yeah, that sucks)
if ($config ["driver"] == 'mysql')
$this->link->exec ( "set names utf8" );
}
catch ( PDOException $e ) {
die ( "Hata var : " . $e->getMessage () );
}
Other:
$encodings = array("windows-1254","utf-8", "iso-8859-9" );
$data = file_get_contents($TempSrc);
$data=mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data, $encodings));
MY SOLUTION
In case anybody experience similar problem, mine was solved by adding this line after the PDO connection , array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'").
Now the connection string looks like this:
$conn = new PDO('mysql:host=localhost;dbname=$dbname', $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
How to rewrite this in pdo:
$con = mysql_connect("localhost:".$LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS);
mysql_select_db("xnews", $con);
mysql_query("set names utf8", $con);
What about something like this :
$db = new PDO('mysql:dbname=xnews;host=localhost;port=' . $LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS);
$db->query('set names utf8');
To open a connection, you have to instanciate PDO, using its constructor, which receives a DSN as first parameter.
And, then, to send queries, you can use the PDO::query method.
Or, for the UTF-8 part, maybe you could use the fourth parameter to PDO::__construct, like this :
$db = new PDO('mysql:dbname=xnews;host=localhost;port=' . $LOCAL_DB_PORT,
$LOCAL_DB_USER,
$LOCAL_DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")
);
See the list of specific stuff for the MySQL Driver, amongst which there is this one :
PDO::MYSQL_ATTR_INIT_COMMAND
Command to execute when connecting to
the MySQL server. Will automatically
be re-executed when reconnecting.