William Kinaan William Kinaan - 1 year ago 107
PHP Question

PDO database for arabic characters

I want to insert Arabic characters to database using pdo with php, but I got these characters in database

الجامع &#1575

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


and this


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'

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:

  1. make the collection of the columns utf8_unicode_ci

  2. put
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    in the header

Answer Source

Try setting the connection to utf-8 when making connection to the database.

  • If you're following rules and best practices:

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:

  • Quick and dirty

Set your connection like this:

$dbhandler = new PDO('mysql:host=your_hostname;dbname=your_dbname;charset=UTF-8', $user, $pass);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download