0

the config php file.

define("DB_SERVER", 'localhost');
define("DB_USER", 'root');
define("DB_PASS", 'pass');
define("DB_NAME", 'db');
define("mysql_query", 'SET CHARACTER_SET utf8');

here is the table structure as the table is been set to utf8_persian_ci, when i insert data from phpmyadmin it's working fine the arabic text gets insert.

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(111)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(122) | NO   |     | NULL    |                |

+-------+--------------+------+-----+---------+----------------+

the query function is kinda like bellow.

////quering
    public function query($sql){
        $this->last_query = $sql;
        $result = mysql_query($sql, $this->connection);
                    $this->confirm_query($result);
        return $result;
    }

but here when i fetch the data from database i get the (?????) type of text. if insert from php input from, when i check the database its (شیبشبشب شبشبش) type of data but when i echo it back from php it show correct but in database the format has changed.

1- if insert from phpmyadmin data to database works fine(means the database character is ok) but can echo back with php (?????)

2- if insert by php from... in database (شیبشبشب شبشبش) but in echo back ok

how i can set the insert form to insert arabic text as is to database(mysql)

how i can echo it back as the data format is in database?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
SAR
  • 1,765
  • 3
  • 18
  • 42
  • What is the charset output of the page? – WizKid May 24 '14 at 19:58
  • local character from page is fine, but when it echo from database not fine – SAR May 25 '14 at 04:41
  • **1.** switch from old mysql_.. api to the newer and supported PDO style (http://php.net/manual/en/book.pdo.php) **2.** make sure you initialize your connection's character set correctly (http://php.net/manual/en/pdo.construct.php) **3.** there are some duplicate SO questions about "PHP PDO MySql utf8" (e.g. http://stackoverflow.com/questions/4854446/save-accents-in-mysql-database). **4.** once you get the utf8 right there should be no more problems (e.g. right-to-left direction should not matter) – xmojmr May 25 '14 at 16:42

1 Answers1

1

we have to set utf8 in connection.php with provides database connection for all:

<?php 
class Database {
    protected $host='localhost';
    protected $user='root';
    protected $db = 'db_test';
    protected $pass = '';
    protected $conn;

    public function __construct(){
        $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
        $this->conn->exec("SET CHARACTER SET utf8");
    }
}
SAR
  • 1,765
  • 3
  • 18
  • 42