Reputation: 28799
I want to insert Arabic characters to database using pdo with php, but I got these characters in database
الجامع ا
and when I use
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
the result was
??????????
I make the collection of the columns in phpMyadmin
utf8mb4_unicode_ci
and this
utf_unicode_ci
but I still have the same error.
And I tried to make the connection like this: (totti,totti is not my really username and password)
parent::__construct('mysql:host=localhost;dbname=ams-competation;charset=utf8"', 'totti', 'totti');
but still I get the same error
And I also tried like this:
parent::__construct('mysql:host=localhost;dbname=ams-competation;charset=utf8', 'totti', 'totti'
,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
and doesn't work :(
and after submitting the form and before inserting to database i echo the values and i got arabic characters , that means my problems is in database , but echo works just without using meta
and when i tried to insert arabic characters from phpmyadmin IT WORKS
i am using
phpmyadmin in XAMPP 1.7.4
php 5.5.3
finally i found the solution and it is:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
in the headerUpvotes: 2
Views: 3814
Reputation: 429
define('DB_CHARSET', 'UTF-8');
not working
use define('DB_CHARSET', 'UTF8');
instead
Upvotes: 1
Reputation: 7336
Try setting the connection to utf-8 when making connection to the database.
Set the constants first in your config file like this:
define('DB_SERVER', 'your_hostname');
define('DB_USERNAME', 'your_username');
define('DB_PASSWORD', 'your_password');
define('DB_CHARSET', 'UTF-8');
define('DB_DATABASE', 'your_dbname');
define('PDO_DSN', 'mysql:host=' . DB_SERVER . ';dbname=' . DB_DATABASE . ';charset=' . DB_CHARSET);
Then set your connection like this:
$_dbhandler = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));
Set your connection like this:
$dbhandler = new PDO('mysql:host=your_hostname;dbname=your_dbname;charset=UTF-8', $user, $pass);
Upvotes: 3
Reputation: 346300
Your problem is most likely not in how you access the database. The characters you see are HTML encoded, so it looks like you're using something like htmlentities()
on your data before writing it to the DB.
Upvotes: 1