Reputation: 57
Hi so I've made an application in java that lets the user to store data in a MySQL database. Whenever I enter this بهرواری دهرچوون I just get ??????. Is there any way I can fix that? It's really important that this job is done before tomorrow... I've never faced a problem such as this, and now I'm creating a database app for a company in Kurdistan, and now my friend asked, well does it store data with their characters? Heart attack! Please help!
I'm using PhpMyadmin on a localhost computer using XAMPP
Upvotes: 1
Views: 1401
Reputation: 63
put the below code in the connection
PHP file then set collation in phpmyadmin (mysql) to utf8_general_ci or utf8_unicode_ci:
<?php
$servername="localhost";
$username="root";
$password="";
$databasename="ab1";
$conn = new mysqli($servername, $username, $password, $databasename);
if ($conn->connect_error) {
die("connection failed" .$conn->connect_error);
}else
echo "connection sucssessfullly";
$conn->query("SET NAMES 'utf8'");
$conn->query("SET CHARACTER SET utf8");
?>
Upvotes: 0
Reputation: 142528
What happened:
SET NAMES latin1
was in effect (default, but wrong)CHARACTER SET latin1
(default, but wrong)As you INSERTed
the data, it was converted to latin1, which does not have values for Arabic (Kurdish/Farsi/etc) characters, so question marks replaced them.
The cure (for future INSERTs
):
mysqli_set_charset('utf8')
(or whatever your client needs for establishing the CHARACTER SET
)CHARACTER SET utf8
<meta...utf8>
should be near the top.The discussion above is about CHARACTER SET
, the encoding of characters. Now for a tip on COLLATION
, which is used for comparing and sorting.
To double check that the data is stored correctly, do
SELECT col, HEX(col)...
.
هرچوون
should come back D987E2808CD8B1DA86D988D988D986
Arabic characters in utf8 have hex of D8xx or D9xx.
(utf8mb4
works just as well as utf8
; either works for Arabic.)
Bad news: The data that was inserted and turned into '???' cannot be recovered.
Upvotes: 2