Unable to output cyrillic ASCII encoding [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have MySQL field which contains plain text Cyrillic characters (ex. Широка поляна). Collation is utf8_general_ci.
When I pull out this content with MySQL query and try to output it with php I always get ???? symbols. HTML encoding is utf8, document encoding is utf8, mb_detect_encoding() shows ASCII for the string but none of the PHP / MySQL convert functions turns it into something readable.

For the outdated mysql driver it have to be
mysql_set_charset('utf8');
for mysqli
$mysqli->set_charset('utf8');
for PDO you have to set encoding in DSN:
$dsn = "mysql:host=localhost;dbname=test;charset=utf8";

You most likely do very common mistake by not setting connection encoding. It's usually done in my.ini config file, but IMHO the better way is to always enforce this in your code. To do so, just execute this query:
SET NAMES encoding;
i.e. for utf8 it would be:
SET NAMES utf8;
You do this just after you connect to database and do it once per connection.

You have to set charset for your database connection.
For this reason you can use mysql_set_charset function.
mysql_set_charset('utf8',$link1);
More information: http://php.net/mysql_set_charset

Use in first line of php file
header('Content-Type: text/html; charset=utf-8');

Related

latin1 to utf8 conversion issue [duplicate]

This question already has answers here:
Trouble with UTF-8 characters; what I see is not what I stored
(5 answers)
Closed 6 years ago.
I have problem with conversion from latin1 to utf8
I have got 2 databases, first is in latin1 second in utf8
Example:
select * from latin1_db gives
"SPÓŁDZIELNIA PRODUCENTÓW TRZODY ODRODZENIE BOBROWNIKI WĄGROWIEC"
but when i insert to utf8 db it becomes
"SPÓ?DZIELNIA PRODUCENTÓW TRZODY ODRODZENIEBOBROWNIKI W?GROWIEC"
how to make that both string will be same
i was using
$str=utf8_encode($str);
$str=Encoding::fixUTF8($str);
and
iconv
but result was not good.
You have to set the database connection encoding with
SET NAMES utf-8
as an sql query. You don't provide the code with the database request, so i cannot update your code to illustrate what i mean. With PDO it should be
$pdo = new PDO(
'mysql:host=yourdbhost;dbname=yourdbname',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
Set database connection encoding to UTF-8.
Also have a look at this answer: Convert utf8-characters to iso-88591 and back in PHP.
mb_convert_encoding();
Might be useful for you.

Superscript character in PHP causing a MySQLi select query to find 0 rows

I am using PHP 5.3.3 and MySQL 5.1.61. The column in question is using UTF-8 encoding and the PHP file is encoded in UTF-8 without BOM.
When doing a MySQLi query with a ² character in SQLyog on Windows, the query executes properly and the correct search result displays.
If I do this same exact query in PHP, it will execute but will show 0 affected_rows.
Here's what I tried:
Using both LIKE instead of =
Changing the encoding of the PHP file to ANSI, UTF-8 without BOM, and UTF-8
Doing 'SET NAMES utf-8' and 'latin1' before running the query
Did header('Content-Type: text/html; charset=UTF-8'); in PHP
Escaping using MySQLi::real_escape_string
Doing a filter_var($String, FILTER_SANITIZE_STRING)
Tried a MySQLi stmt bind
The only way I could get it to work properly is if I swapped the ² for a % and changed = to LIKE in PHP.
How can I get it query properly in PHP when using the ²?
You should be able to get the query to work by ensuring the following:
Prepping PHP for UTF-8
You first need to make sure the PHP pages that will be issuing these queries are served as UTF-8 encoded pages. This will ensure that any UTF-8 output coming from the database is displayed properly. In Firefox, you can check to see if this is the case by visiting the page you're interested in and using the View Page Info menu item. When you do so, you should see UTF-8 as the value for the page's Encoding. If the page isn't being served as UTF-8, you can do so one of two ways. Either you can set the encoding in a call to header(), like this:
header('Content-Type: text/html; charset=UTF-8');
Or, you can use a meta tag in your page's head block:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Prepping MySQL for UTF-8
Next up, you need to make sure the database is set up to use the UTF-8 encoding. This can be set at the server, database, table, or column levels. If you're on a shared host, you probably can only control the table and column levels of your hierarchy. If you have control of the server or database, you can check to see what character encoding they are using by issuing these two commands:
SHOW VARIABLES LIKE 'character_set_system';
SHOW VARIABLES LIKE 'character_set_database';
Changing the database level encoding can be done using a command like this:
(CREATE | ALTER) DATABASE ... DEFAULT CHARACTER SET utf8;
To see what character encoding a table uses, simply do:
SHOW CREATE TABLE myTable;
Similarly, here's how to change a table-level encoding:
(CREATE | ALTER) TABLE ... DEFAULT CHARACTER SET utf8;
I recommend setting the encoding as high as you possibly can in the hierarchy. This way, you don't have to remember to manually set it for new tables. Now, if your character encoding for a table is not already set to UTF-8, you can attempt to convert it using an alter statement like this:
ALTER TABLE ... CONVERT TO CHARACTER SET utf8;
Be very careful about using this statement! If you already have UTF-8 values in your tables, they may become corrupted when you attempt to convert. There are some ways to get around this, however.
Forcing MySQLi to Use UTF-8
Finally, before you connect to your database, make sure you issue the appropriate call to say that you are using the UTF-8 encoding. Here's how:
$db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Change the character set to UTF-8 (have to do it early)
if(! $db->set_charset("utf8"))
{
printf("Error loading character set utf8: %sn", $db->error);
}
Once you do that, everything should hopefully work as expected. The only characters you need to worry about encoding are the big 5 for HTML: <, >, ', ", and &. You can handle that using the htmlspecialchars() function.
If you want to read more (and get links to additional resources), feel free to check out the articles I wrote about this process. There are two parts: Unicode and the Web: Part 1, and Unicode and the Web: Part 2. Good luck!

Cant retrieve greek characters from mysql database with PHP [duplicate]

This question already has answers here:
PHP MySQL Greek letters showing like ???? marks
(6 answers)
Closed 9 years ago.
currently on Wamp 2.20
MySQL version : 5.5.20
PHP version : 5.3.10
table collation : utf8-bin
header('Content-type: text/html; charset=utf-8');
while i can see the data saved in the MySQL table are the Greek characters, when i try to echo them from PHP, they turn into "?" question marks.
Make sure your client connection is set for UTF8. Examples:
SQL
SET NAMES UTF8;
PHP MySQLi
mysqli_set_charset('utf8');
PHP PDO
$handle = new PDO("mysql:host=localhost;dbname=dbname",
'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
PHP mysql (deprecated - do not use)
mysql_set_charset('utf8');
Try using htmlentities to encode your special characters before echo'ing your data.
echo htmlentities($data);
Be sure that your file are saved using ANSI as UTF-8 aka UTF-8 without BOM. You can do it using NotePad++ : http://npp-community.tuxfamily.org/documentation/notepad-user-manual/document-properties/encoding
Also, when working with UTF-8 (Unicode) please remember :
All files must be saved using UTF-8 encoding ;
MySQL tables must be in UTF-8 ;
Fields in MySQL must be UTF-8 ;
PHP must be set to use UTF-8 ;
Everything has to be UTF-8 encoded.

Cant insert utf8 characters on mysql (with utf8 collation, charset and nameset)

im facing a really stressing problem here.. i have everything in UTF-8 , all my DB and tables are utf8_general_ci but when trying to insert or update from a single PHP script all i see are symbols.. but if i edit in phpmyadmin the words are shown correctly.. i found that if i run the utf8_decode() function to my strings in php i can make it work, but im not planning to do that because is a mess and it should work without doing that :S
Here is a basic code im using to test this:
<?php
$conn=mysql_connect("localhost","root","root")
or die("Error");
mysql_select_db("mydb",$conn) or
die("Error");
mysql_query("UPDATE `mydb`.`Clients` SET `name` = '".utf8_decode("Araña")."' WHERE `Clients`.`id` =25;",
$conn) or die(mysql_error());
mysql_close($conn);
echo "Success.";
?>
This is what i get if i dont decode utf8 with php utf8_decode function:
instead of Araña, i get : Araña
I've run into the same issue many times. Sometimes it's because the type of database link I'm selecting from isn't the same type that I'm using for inserting and other times, it's from file data into a database.
For the later instance, mysql_set_charset('utf8',$link); is the magic answer.
Place the call to mysql_set_charset just after you select your database via mysql_select_db.
#ref http://php.net/manual/en/function.mysql-set-charset.php
"Araña" IS UTF-8. The characters "ñ" represent the two bytes into which the Spanish ñ are encoded in UTF-8. Whatever you're reading it back with is not handling the UTF-8 and is displaying it as (it appears) ISO-8859-1.
That DDL you mentioned has to do with the collation, not the character set. The correct statement would be:
ALTER TABLE Clients CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
You still need to make sure the client library (libmysql or whatever driver PHP is using) is not transcoding the data back to ISO-8859. mysql_set_charset('utf8') will explicitly set the client encoding to UTF-8. Alternatively, you can send a SET NAMES UTF8; right after you connect to the database. To do that implicitly, you can change the my.cnf [client] block to have utf-8 as the client character encoding (and /etc/init.d/mysql reload to apply). Either way, make sure the client doesn't mangle the results it's pulling.
[client]
default-character-set=utf8
You do not need to use utf8_decode if you're using mbstrings. The following php.ini configuration should ensure UTF-8 support on the PHP side:
mbstring.internal_encoding = utf-8
mbstring.http_output = utf-8
mbstring.func_overload = 6
Finally, when you display the results in HTML, verify that the page's encoding is explicitly UTF-8.

UTF-8 problems PHP/MySQL

I've always used ISO-8859-1 encoding, but I'm now going over to UTF-8.
Unfortunately I can't get it to work.
My MySQL DB is UTF-8, my PHP document is encoded in UTF-8, I set a UTF-8 charset, but it still doesn't work.
(it is special characters like æ/ø/å that doesn't work)
Hope you guys can help!
Make sure the connection to your database is also using this character set:
$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);
According to the documentation of mysql_set_charset at php.net:
Note:
This is the preferred way to change the charset. Using mysql_query() to execute
SET NAMES .. is not recommended.
See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php
Check the character set of your current connection with:
echo mysql_client_encoding($conn);
See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php
If you have done these things and add weird characters to your table, you will see it is displayed correct.
Remember to set connection encoding to utf8 as well.
In ext\mysqli do
$mysqli->set_charset("utf8")
In ext\mysql do
mysql_set_charset("utf8")
With other db extensions you might have to run query like
SET NAMES 'utf8'
Some more details about connection encoding in MySQL
As others point out, making sure your source code is utf-8 encoded also helps. Pay special attention to not having BOM (Byte Order Mark) - it would be sent to browser before any code is executed, so using headers or sessions would become impossible.
After connecting to db, run query SET NAMES UTF8
$db = new db(...);
$db->query('set name utf8');
and add this tag to header
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Are you having this error? MySql SELECT UNION Illegal mix of collations Error? Just set you entire mysql to utf 8 then
SET character_set_connection = utf8;
Try this after connecting to mysql:
mysql_query("SET NAMES 'utf8'");
And encode PHP document in UTF-8 without BOM.
I had the same problem but now its resolved. Here is the solution:
1st: update ur table
ALTER TABLE tbl_name
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
2nd:
add this in the head section of the HTML code:
Regards
Saleha A.Latif
Nowadays PDO is the recommended way to use mysql. With that you should use the connection string to set encoding. For example: "mysql:host=$host;dbname=$db;charset=utf8"

Categories