William Kinaan
William Kinaan

Reputation: 28799

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

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:

  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

Upvotes: 2

Views: 3814

Answers (3)

Mahmoud Kassem
Mahmoud Kassem

Reputation: 429

define('DB_CHARSET', 'UTF-8'); not working

use define('DB_CHARSET', 'UTF8'); instead

Upvotes: 1

Stacked
Stacked

Reputation: 7336

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:

$_dbhandler = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));
  • Quick and dirty

Set your connection like this:

$dbhandler = new PDO('mysql:host=your_hostname;dbname=your_dbname;charset=UTF-8', $user, $pass);

Upvotes: 3

Michael Borgwardt
Michael Borgwardt

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

Related Questions