SAR
SAR

Reputation: 1845

PHP- can not able to insert and get arabic data from mysql

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?

Upvotes: 0

Views: 1143

Answers (1)

SAR
SAR

Reputation: 1845

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");
    }
}

Upvotes: 1

Related Questions