du5591 2014-04-11 01:24
浏览 43

将persian值插入到具有波斯名称的表时出错

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

  • 写回答

1条回答 默认 最新

  • douchuanghan1344 2014-04-11 01:30
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因