Unicode characters become question marks after inserting into the database - php

When I insert some text written in Unicode into database, they become question marks. Database encoding is set to UTF-8. What else may be incorrect? When I check in phpMyAdmin there are question marks inserted only!
This is the code I use for connecting to database:
define ("DB_HOST", "localhost"); // Set database host
define ("DB_USER", "root"); // Set database user
define ("DB_PASS","password"); // Set database password
define ("DB_NAME","name"); // Set database name
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
mysql_set_charset('utf8',$link);
mysql_query("SET CHARACTER SET utf8");

Is the text you inserted encoded in UTF-8 too? Or is your PHP files not UTF-8? Have you set the MySQL Client connection to UTF-8?
If not, then that is probably the cause of the problem.

How do you know the become question marks? Do you see them as question marks on your PHP pages, when you output the database fields, or in software like phpMyAdmin?
Either way, the problem is probably the encoding of your web page rather than the database's. Make sure to add the following line:
header('Content-Type: text/html; charset=utf-8');

// First make sure your file produce UTF-8 characters
header('Content-Type: text/html; charset=utf-8');
// Make sure with your spelling
// write
mysql_query("SET CHARSET utf8");
// Instead of
mysql_query("SET CHARACTER SET utf8");
// For some reasons
mysql_query("SET CHARSET SET utf8");
// It works on some servers and for other servers not. I am not sure why.
// Try using mysql_set_charset("utf8"); only without mysql_query("SET CHARSET utf8");
// For me I had the same issue with my server
// When I used mysql_set_charset("utf8"); only --> the problem solved
// again make sure with your spelling and try again

Sorry, but you are all wrong...
My friend King Julien, you just have to execute:
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET CHARSET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'"); // This statement does the job!!! ;)
Have a nice day!

Related

spanish word is not displaying as it is in html [duplicate]

I have a table where the data are stored like boîtes with none utf8 characters. Now I have my php script which works fine on my local machine.
$utf = utf8_decode($details);
echo "UTF8-DE : ".$utf."<hr>";-> `boîtes`
When I put this script on another machine its not working its echoing boîtes . I am sure it depends on the charset of the php or server? Any help please
Try set_charset function
For mySqli
mysqli_set_charset($connection, "utf8");
For mySql
mysql_set_charset("UTF8", $connection);
Alternative
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
Set it up with headers
header('Content-type: text/html; charset=utf-8');

Character encoding php mysql

I have a table where the data are stored like boîtes with none utf8 characters. Now I have my php script which works fine on my local machine.
$utf = utf8_decode($details);
echo "UTF8-DE : ".$utf."<hr>";-> `boîtes`
When I put this script on another machine its not working its echoing boîtes . I am sure it depends on the charset of the php or server? Any help please
Try set_charset function
For mySqli
mysqli_set_charset($connection, "utf8");
For mySql
mysql_set_charset("UTF8", $connection);
Alternative
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
Set it up with headers
header('Content-type: text/html; charset=utf-8');

MySQL cannot recognize Korean characters

I need to insert korean text to my database which is coming by url get request. However, inserted values are not recognized in MySQL. Please, need quick instruction. Thanks.
setlocale(LC_CTYPE, 'ko_KR.utf8');
mb_internal_encoding("UTF-8");
mb_http_output('utf-8');
$p_text = rawurldecode($_GET["text"]);
Right after your mysql connection make this query
$mysqli->query("SET NAMES 'UTF8'");
or
mysql_query("SET NAMES 'UTF8'");
also make sure that your fields have utf8_general_ci collation

Mysql php character set setting

At first we was using the default character set by the mysql that was latin1_swedish_ci. Then we had problem with other languages like russian etc. So we have now converted the tables into collation and the field into utf8_unicode_ci. We need some confirmation here should we stick with utf8_unicode_ci for the support all the possible languages or go for something else like utf8_bin?
Next issue for php files where we are using mysqli we have set this.
if (!mysqli_set_charset($link, "utf8")) {
echo("Error loading character set utf8: ". mysqli_error($link));
}
else {
echo("Current character set: ". mysqli_character_set_name($link));
}
Then for places where we use mysql only we did as below
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
The problem now we find this is like quite tedious and we are scared we might be miss. Is it possible to set this character setting in like a config file like we have one for our db connection?
Don't mix mysql_* with mysqli_* functions. You need to stay consistent! You use mysqli_ first and then you use mysql_. That won't work!
This is how I do it:
mysqli_set_charset($Handle, 'utf8'); // <- add this too
mysqli_query($Handle, "SET NAMES 'utf8';");
mysqli_query($Handle, "SET CHARACTER SET 'utf8';");
mysqli_query($Handle, "SET COLLATION_CONNECTION = 'utf8_unicode_ci';");
// might be a bit redundant but it's safe :) ... I think :)
Then make sure you provide proper UTF8 to it.

mysql_fetch_array returns non-Unicode text

I've made a simple PHP page to get the POST data and fetch a sql query then print the result. I'm using the mysql_fetch_array function.
The code works great but the response is a non-Unicode text, and it returns something like this:
?????ABC?????
note that the database collation is UTF8 and data stored are shown correctly in phpMyAdmin. I even used this META tag in php page but it results the same:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
Any idea?!
Add these lines of code before the first query:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET SESSION collation_connection = 'utf8_unicode_ci'");
Or you can edit your mysql configuration to use utf8 by default. Check here for what you need to do.
Change MySQL default character set to UTF-8 in my.cnf?
UPDATE
The function mysql_query is deprecated, so mysqli object can be used like so:
$mysqli = new mysqli("localhost", "MYSQL_USER", "MYSQL_PASS", "MYSQL_DB");
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");
$mysqli->query("SET SESSION collation_connection = 'utf8_unicode_ci'");
mysql_set_charset('utf8', $link);
Where $link is a connection created with mysql_connect
Try using mb_internal_encoding("UTF-8"); for details http://php.net/manual/en/function.mb-internal-encoding.php

Categories