mysql_fetch_array returns non-Unicode text - php

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

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

Utf8 - How to clean these strings?

i use
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
Mysql database with "utf8_general_ci"
but i get these strings
Saddle Club 3ã�​â&de​g;
Speciale Aldo Moro02/04/�
How to clean all these strings?
You need to set the encoding of your mysql database connection.
$this->mysqli->query('SET NAMES utf8'); //on your mysqli instance
or
mysql_query('SET NAMES utf8'); // simple mysql query
Don't know what type of connection do you use, but that might help..
Do you use any utf-8 settings upon your queries?
Like:
mysql_query("SET CHARACTER SET utf8", $this->connection);
mysql_query("SET NAMES utf8", $this->connection);
$result = mysql_query($sql, $this->connection);

Unicode characters become question marks after inserting into the database

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!

Categories