Clemems Wichmann
Clemems Wichmann

Reputation: 21

storing arabic text in mysql using pdo in php

I'm working on arabic site and for that I want to store the arabic input in database. I've set the character set to utf8mb4_general_ci. When I'm printing the data before the insert query, then it is showing me correct arabic value. But when I am inserting it into db it is storing as اÙرÙاضâ. I am using PDO in PHP and I've also set the character set to utf 8 in connection string.

$this->pdo = new PDO($dsn, $this->settings["user"],   
$this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

But I am not able to store arabic character in my table.

Upvotes: 0

Views: 898

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157828

When setting client charset, one have to make it match the actual data encoding.

So, if your input data is in utf-8, everything should work, but in this case why would you set database charset to utf8mb4, not utf8?

If your input data encoding is different from utf-8, then you have to set names to match this actual encoding.

Also, setting charset in PDO::MYSQL_ATTR_INIT_COMMAND is but a superstition. Although in most cases it plausible, better set it via DSN - it works for all the currently supported PHP versions. Note that encoding names are slightly different from commonly used.

Regarding strange characters you're observing - it's most likely no more than measurement error. The tool you are using to browse the database, have to both support that encoding and set up to display it properly.

All the above is based on the assumption that

I'd set the character set to utf8mb4_general_ci.

statement is about setting the table charset.

Upvotes: 1

Related Questions