2

I have a column with nickname that it's collection is utf8-general-ci and table collection is UTF-8. But persian/arabic text stores in it like below.

https://i.stack.imgur.com/CCWOa.png

How should Solve this problem?

I use this code to save

$sql = "INSERT INTO kashef_users(nickname ,username ,email ,ostan ,age ,gender ,sdk ,phonename ,imei ,time ) VALUES ".$data;

$data contains a a string and everything in data is correct

ali
  • 63
  • 1
  • 7
  • Are you sure that's not just a bug in the application that displays it? If it's really stored wrong, it was most likely inserted wrong, so you'll need to show the code that does the insert as well as any other code that works with the string before it's inserted. – Wyzard Dec 13 '14 at 15:37
  • use `uft-8-persian` rather than `General-ci` –  Dec 13 '14 at 15:48
  • "Everything in data is correct" — are you sure that `$data` is in the encoding that the database expects? Your code looks like it may be PHP, and PHP doesn't have native Unicode strings, so you have to make sure the string is in the proper encoding (e.g. UTF-8), and configure the database connection to use the same encoding. – Wyzard Dec 13 '14 at 15:50

1 Answers1

6

It seems you are using php to populate $data. So you have to set charset as UTF8 (i.e. SET NAMES 'utf-8';). For example if you were using mysqli, your code would be like this:

<?php
    $conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
    mysqli_set_charset($conn,"utf8");

Or use this answer if you were using PDO or this one for deprecated mysql. Moreover, do not forget to use utf8-persian-ci collection.

In your html if you have a form, set the accept-charset as UTF-8:

<form action="" accept-charset="UTF-8">
Community
  • 1
  • 1