0

I have a simple class that should insert my values to my mysql table.

The Tables will make by administrator and table names would be in Persian , for example :

DBNAME===>aalborz
TableName==>سارا

enter image description here

This is my CLASS:

<?php
class insertMachine {

    private $_username;
    private $_password;
    private $_dbName;
    private $_tableName;
    private $_host;

    protected $_connection;
    protected $_fields = array();
    protected $_colNames = array();
    protected $_colValues = array();

    public    $_report;


    public function __construct($dbName = 'aalborz', $tableName, $inputValues ) {

        if ( !is_array($inputValues) ) {
            echo 'The Last argument have to be an ARRAY...!!!';
            exit();
        }//if
        else {
            foreach ( $inputValues as $key=>$val ) {

                $this->_colNames[] = $key;
                $this->_colValues[] = $val;



            //  var_dump($this->_colNames);

            }//foreach 

            /*var_dump($this->_colNames, $this->_colValues);*/
        }//else

        $this->_host = 'localhost';
        $this->_dbName =  $dbName;
        $this->_tableName = $tableName;
        $this->_username = 'aalborz';
        $this->_password = 'password';
        $this->_fields = $inputValues;

        try {
            $this->_connection = new PDO('mysql:host = ' . $this->_host . '; dbname = ' . $this->_dbName, $this->_username, $this->_password);
            $this->_connection->exec("SET SESSION collation_connection = 'utf8_general_ci';");
            $this->_connection->exec("SET CHARACTER SET 'utf8';");
            $this->_connection->exec("SET character_set_server=utf8");
            $this->_connection->exec("set names utf8 COLLATE 'utf8_general_ci'");

            /*echo 'Connect shodi';*/
        }//try
        catch( Exception $e ) {
            echo '<pre dir="ltr">';
            die($e);
            echo '</pre>';
        }//catch


    }//__construct

   public function makeQuery() {

        $sql = "INSERT INTO `" . $this->_dbName . "`.`" . $this->_tableName . "` (";

        $numberOfCols = count($this->_colNames);
        $numberOfVals = count($this->_colValues);

        for($i = 0; $i <= $numberOfCols - 1; $i++) {

            if ( $i != $numberOfCols -1 ) {

                $sql .=  "'" . $this->_colNames[$i] . "', ";

            }//if

            else {

                $sql .= "'" .  $this->_colNames[$i] . "') ";

            }//else

        }//for Loop for making first part of Query

        $sql .= " VALUES(";

        for ( $j = 0; $j <= $numberOfVals - 1; $j++) {

            if ( $j != $numberOfVals -1 ) {

                $sql .=  ":" . $this->_colNames[$j] . ", ";
                //$sql .= '? ,';
            }//if
            else {

                $sql .=  ":" . $this->_colNames[$j] . ") ";             
                //$sql .= '? )';

            }//else

        }//for Loop for making Second  part of Query


        //prepare query for insert values
        $prepare = $this->_connection->prepare($sql);

        //execute method for PDO object
        $exe = $prepare->execute( $this->_fields );


        if ( $exe ) {
            $this->_report = '<span style="color:green;">Success...</span>';
        }//if $exe
        else {
            $this->_report = '<span style="color:red;">Fail, Try Again...!!!</span>';
        }//else
        /*echo '<hr />';*/
        echo '<pre dir="ltr">';
        var_dump($sql, $prepare, $exe);
        echo '</pre>';
    }//makeQuery function 


}//CLASS
?>

When I use a table with ENGLISH name, everything is fine, BUT when I test it with persian table, the insert will fail...

Would you tell me how should I fix this...???

I checked this links and questions for my answer but they are not my answer:

Insert a persian text in mysql table
Persian character's issue in mysql database
Persian words in the database and it is issue?
Searching Persian characters and words in SQL server with different encoding
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_character_set_connection
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_character_set_client

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kiyarash
  • 2,437
  • 7
  • 32
  • 61
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – jeroen Apr 11 '14 at 01:36
  • 2
    I'm checking, thanks for warning, I will delete my post if that was my problem... – Kiyarash Apr 11 '14 at 01:40
  • I'm not completely sure as I have never used utf8 characters in table- or column names, but it is a pretty complete guide / checklist. – jeroen Apr 11 '14 at 01:54
  • I'm trying since 10 hours ago and I have test so many different ways, I'm confused... That question didn't help either, but thank you again – Kiyarash Apr 11 '14 at 02:05

1 Answers1

0

You should set UTF-8 character set for your tables.

Database Chartset = utf8 & Database Collation = utf8_general_ci . 

And you should permanently do it for your tables

That should work! Check below link if it helps as well:

Insert a persian text in mysql table

Community
  • 1
  • 1
kk497055
  • 116
  • 8