Send persian string to mysql Send persian string to mysql database database

Send persian string to mysql


Before insert Unicode character like persian or arabic in database you need to set Utf-8 characterset on db connection.

public function url($new){$db = Db::getInstance();$db->exec('SET NAMES utf8');  $db->insert("INSERT INTO table (column) VALUES ('$new')");}

also for prevent any problem you must set database collation to utf8 general ci when creating databases that store persian characters or other unicode characters.


Modify your table

ALTER TABLE 'your table name' MODIFY 'column name' VARCHAR(20) CHARACTER SET UTF8;

For displaying Persian on your webpages you need to use code

<meta charset="UTF-8">

For URL :

$string = '%E0%A4%89-%E0%A4%85%E0%A4%B9%E0%A4%BF%E0%A4%B2%E0%A5%87-%E0%A4%95%E0%A4%B9%E0%A4%BE%E0%A4%81-%E0%A4%9B-%E0%A4%A4';

echo rawurldecode($string);


There are three layers that can be causing your problem.Firstly, your database will have a default character encoding.Secondly, your table can have an explicit character encoding which takes precedence. So you need to make sure you have a Unicode encoding set for your table through one of these methods. You say your table is set to utf8_general_ci so this part should be fine.Thirdly, there is the connection itself which has a specific encoding. This can be set up in MySQL client configuration defaults with the following: [mysqld] init_connect='SET NAMES utf8'

You can also set it in the code. For PHP for example, you might do the following: mysql_set_charset("UTF8", $conn);or $db->exec( 'SET CHARACTER SET UTF8' )

You haven't said anything about what $db is, so can't be certain. But something like that.

Finally, make sure it's not actually working but you just don't know it. Your web page should be set to utf8. Check what you're actually receiving from the web page and don't just assume it's sending Unicode.