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

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.

Related

Updating MySQL table with non-english characters not working, collation and charset are all set to utf8 or utf8mb4 [duplicate]

This question already has answers here:
Trouble with UTF-8 characters; what I see is not what I stored
(5 answers)
UTF-8 all the way through
(13 answers)
Closed 2 years ago.
I am having an issue trying to update my database, I believe related to a charset / collation issue. I've searched all the other related issues, updated collations and charsets, tried everything, and nothing seems to work.
What I'm doing is getting data from scraping some HTML (with permission from the site owner), manipulating it a bit, and then doing an UPDATE to save the manipulated data in my table.
I have a field, reference, that is taken from the HTML, and the update looks for that field, and updates my table if the field matches. If there are no special (non-english) characters, it works fine:
UPDATE database.table SET points = 100 WHERE reference = 'Real Madrid'
If there are any non-english characters in the reference, then the update doesn't work IF I do it from my PHP / HTML site - if I put the query below directly into phpmyadmin, it works fine:
UPDATE database.table SET points = 100 WHERE reference = 'Atlético Madrid'
This happens with every non-english character I've tried, not just é, so that seems to be the root issue.
The HTML I ingest is initially UTF-8, but at some point, it seems the encoding of my text is getting changed from straight UTF-8 to ASCII. Is ASCII not a subset of UTF-8? Not entirely sure if that's the problem, but the encoding is different, which is odd.
Below is my code, with encoding pointed out at different times:
$html = file_get_html('http://url.to.scrape');
// At this point, `mb_detect_encoding($html)` is UTF-8.
$i = 1;
while($i <= 20){
foreach($html->find('tr') as $tableRow) {
// At this point, `mb_detect_encoding($tableRow) is `ASCII`
$rowData['team'] = $tableRow->find('td', 0)->plaintext;
// At this point, `mb_detect_encoding($rowData['team']) is `ASCII`
$rowData['points'] = $tableRow->find('td', 1)->plaintext;
$points = $rowData['points'] * doSomeManipulationHere();
$update_query = "UPDATE database.table SET points = $points WHERE reference = '". $rowData['team'] ."'";
print_r($update_query);
}
}
As mentioned, if $rowData['team'] does not contain non-english characters, it works. If it does contain any, it doesn't.
Again, as mentioned, If I print_r($update_query), and I copy / paste the output directly into phpmyadmin in the SQL tab, it works as expected, even with the é character, so that makes me believe that MySQL charset / collation is set up correctly, and it's somewhere in the PHP / HTML / MySQL connection that's causing the issue.
I guess I need to figure out why my data is suddenly ASCII when is started out as UTF-8.
My setup:
MySQL Server connection collation: utf8mb4_unicode_ci
MySQL Table collation: utf8mb4_unicode_ci
MySQL Field collation: utf8mb4_unicode_ci
PHP default Charset: UTF-8
HTML: <meta charset="utf-8">
.htaccess / charset.conf: AddDefaultCharset UTF-8 (edit: added after originally posted, thanks for the suggestion #asiri)
I've tried sending header('Content-Type: text/html; charset=utf-8');, which didn't help.
I am also seeing the dreaded black question mark � when I view those characters on the site, so it's gotta be the encoding somewhere, I just don't know where.
You can try setting up encoding in your .htaccess file.
AddDefaultCharset utf-8
Add this line into the .htaccess file inside the root directory where you have placed your code.
or
try this. The below code will set the encoding into your database connection.
$link = mysqli_connect('localhost', 'user', 'password', 'database');
mysql_set_charset('utf8',$link);

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.

MySQL & PHP special character issue [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I am using MySQL 5.5 version
when i try to insert the ‘ special character in database it is automatically converted into ’ .
i changed the database character set to utf8 & character_set_connection to utf8 but i unable to get the expected result.
how to solve this issue ?
kindly help on this
You need to check how you are sending the data.
If the character set in the database is utf-8, you need to send like that to.
Try to encode the data before, like that:
$sql = "INSERT INTO tablex(field) VALUES('".utf8_encode($mydata)."')";
It is important to make sure that every part of your connection is using utf8, otherwise you will run into problems.
Below we will create a utf8 connection to the database, perform set names which is vitally important and then write using a utf8_encode method.
mysql_connect("host", "user", "pass");
mysql_query("SET character_set_results=utf8");
mysql_set_charset('utf8');
mb_internal_encoding('UTF-8');
mysql_select_db("my_db");
mysql_query("set names 'utf8'");
$sql = "INSERT INTO `table`(`foo`) VALUES('".utf8_encode($bar)."')";

Unable to output cyrillic ASCII encoding [duplicate]

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');

Diacritics from a mysql database not displayed correctly with an HTML and PHP page

I have a mysql database (Wamp) which uses latin1_swedish to encode characters (é, è, ...) but for an unknown reason when I display some results from the database (using an HTML page and a PHP page) the diacritics (à, é, è) are not displayed correctly.
I've already tried to change the charset of my webpage (iso 88599-1, utf-8) but it doesn't work.
echo htmlentities($variable_name);
Try to check the encoding on your files (you can do this with notapad++), in your database (you can see this in phpmyadmin), and your database connection (you can check this in your php file that connects to the db).
They should all match
If you know the character encoding that comes in and you know what goes out, you may be able to use iconv: http://www.php.net/manual/en/function.iconv.php
For example if the db is iso-8859 and you need utf-8, you call
iconv("ISO-8859-1", "UTF-8", $text);
You can't just change the character set of a table once values are stored, because the data will not be converted. You will have to convert the data using the available MySQL functions. Read more about that at http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html.
Did you set your connection to mysql db with the
SET NAMES utf8;
command?
There are several ways:
mysql_query("SET NAMES 'utf8'");
and
mysql_query("SET CHARACTER SET utf8 ");
Or, the "best";
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

Categories