Arabic language in php/mysql appears "????" question marks in html [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Save Data in Arabic in MySQL database
I have a problem with retrieving Arabic data from MYSQL database using PHP, it appears as question marks "????" in HTML:
I have a database with "utf8_general_ci" as collation.
The database contains some data in Arabic Language.
The HTML encoding is "UTF-8".
When I tried to retrieve the data in HTML, it appears as "?????".
Please Help !!!

you must set charset in first connect with mysql by this query:
SET CHARACTER SET utf8
for example in mysqli functions
$MySQL_Handle = mysqli_connect(HOSTNAME,DATABASE_USERNAME,DATABASE_PASSWORD,DATABASE_NAME)
or die ( mysqli_error($MySQL_Handle) );
$sSQL= 'SET CHARACTER SET utf8';
mysqli_query($MySQL_Handle,$sSQL)
or die ('Can\'t charset in DataBase');
and PDO sample :
$dbh = new PDO('mysql:host=localhost;dbname=' . $DB_NAME, $DB_USER,
$DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$dbh->exec("SET CHARACTER SET UTF8");
this action need before insert and before select.

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.

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

MySQL database encoding with php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have all tables and database encoded with utf8_general_ci and all data in database are ok.
When I select data with PHP (PDO) and print in CLI (and when I save these data into db) texts contains question marks:
Raw, zagra? te? rol? wiceprezydenta
Why ? What's wrong ?
I tried SET NAMES solution but don't work.
Part of my code:
<?php
setlocale(LC_ALL, 'pl_PL');
ini_set('default_charset','utf-8');
mb_internal_encoding("UTF-8");
putenv('LANG=pl_PL.UTF-8');
$db = new PDO('mysql:host=localhost;dbname=nameofdb;charset=utf-8','root','******', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// body...
The charset in your $db connection string should be utf8 not utf-8 like this:
$db = new PDO('mysql:host=localhost;dbname=nameofdb;charset=utf8','root','****',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

How to properly display non-redable characters stored in a MySQL Database

I have São Tomé and Príncipe stored in a MySQL database.
It displays on my web page as S�o Tom� and Pr�ncipe
Is there anyway to display it properly straight from the database?
My MySQL Type is MyISAM and the collation is utf8_general_ci
My web page charset is <meta charset="utf-8">
Field type is set to varchar(30) and field collations is same as table utf8_general_ci
PHP FUNCTION TESTS
utf8_decode() converted characters to S?o Tom? and Pr?ncipe
htmlspecialchars_decode() converted characters to S�o Tom� and Pr�ncipe
html_entity_decode() converted characters to S�o Tom� and Pr�ncipe
quoted_printable_decode() converted characters to S�o Tom� and Pr�ncipe
htmlentities() returned blank result
GOT IT TO WORK THANKS TO #SAEVEN
Ran this right before my query SELECT
mysqli_query($GLOBALS['db_link'], "SET NAMES 'utf8'");
Could it be that you need to specify your namespace collation at the connection level?
I assume that you've covered the basics, you've got the charset as utf8 in your HTML markup.
Try executing this SQL statement before you do any of your database queries:
'SET NAMES utf8'
e.g., if you are using PDO
// do your regular connection logic whatever it is
$dbc = new PDO( .... );
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbc->query( 'SET NAMES utf8' );
// run your queries beneath
....
or can use the shorthand
new PDO(
'mysql:host=mysql.example.com;dbname=awesome_db',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
Whatever DBAL you use, that query'll work. Just run it on the same connection you use to pull your data, before pulling it (only needs to be done once)

Thai characters into UTF8 Database [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I tried to put some thai sings into a utf8 (utf8_general_ci) mysql database. The data is from the facebook api. In the JSON it looks like \u0e41\u0e15\u0e07\u0e08\u0e49 (original: แตแจ้) and in the database i got some ???? (question marks).
What is the best solution to save this characters into a database?
This is my database connection with mysqli:
$DB = new mysqli('localhost', 'XXX', 'XXXXXX', 'XXXX');
$DB->query("SET CHARACTER SET 'UTF8'");
$DB->set_charset("UTF8");
ok, i found the sulotion....
the table was set to utf8_general_ci but not the utf8_general_ci table column... it was latin1....
in your db table, set your column collation to utf8_unicode_ci, then :
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");

Categories