PDO¢/ TradeMark - 编码问题UTF8

时间:2015-10-28 11:17:12

标签: php pdo encoding utf-8

数据库类 [另存为utf8]

private function Connect()
        {
            $this->settings = parse_ini_file("settings.ini.php");
            $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].';charset=utf8';
            try 
            {
                # Read settings from INI file, set UTF8
                $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

                # We can now log any exceptions on Fatal error. 
                $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                # Disable emulation of prepared statements, use REAL prepared statements instead.
                $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

                # Connection succeeded, set the boolean to true.
                $this->bConnected = true;
            }
            catch (PDOException $e) 
            {
                # Write into log
                echo $this->ExceptionLog($e->getMessage());
                die();
            }
        }

core.php [文件另存为utf8] 在上面有

ini_set('default_charset','utf-8');
header('Content-type: text/html; charset=utf-8');

我为核心类添加了一个记录器,它返回$ param = Item:Item™%204125%20 |%20Something

public function item_price_check($param){
 $param = str_replace("\"", "", $param);
            $param = str_replace("\"", "", $param);
            $param = str_replace(" ", "%20", $param);
            $param = str_replace("\\", "", $param);
            // $param = json_decode($param);

            $itemDetails = $db->query("SELECT * FROM items WHERE name = :param", array(
                "param" => "" . $param . ""
            ));
 if (empty($itemDetails)) { //no add on
                $get_Price = $this->price_check_url($param);
                //Part responsible for database insert
                $insert    = $db->query("INSERT INTO items(cost,lastupdate,name) VALUES(:cost,:lastupdate,:name)", array(
                    "cost" => $get_Price,
                    "name" => $param,
                    "lastupdate" => time()
                ));
.........
}

cost.php [文件另存为utf8]

<?PHP
ini_set('default_charset','utf-8');
header('Content-type: text/html; charset=utf-8');
include('lib/core.php');
$db = new Db();
$core = new core();

$core->item_price_check($_GET['item']);
?>

Headers

Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:30
Content-Type:text/html; charset=utf-8
Date:Wed, 28 Oct 2015 11:00:07 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.22 (Debian)
Vary:Accept-Encoding
X-Content-Type-Options:nosniff
X-Powered-By:PHP/5.4.44-0+deb7u1
X-XSS-Protection:1 ;mode=block

Query Param string : 
item:Item™ 4125 | Something

数据库设置

Table :: MyISAM utf8_general_ci
Structure :: name   text    utf8_general_ci

总结 - 问题

每次我使用包含商标符号的$ _GET访问cost.php文件时,它将被转换为数据库中的符号,我认为我已经完成了所有操作,所有这些都是utf8 - 连接,文件。你知道这里有什么不对吗?我也尝试在插入许多函数htmlentitles,utf8编码,mb_convert为$ param - 但每次它返回一个â€。 谢谢你的任何建议。

1 个答案:

答案 0 :(得分:0)

答案如下:

我一直在使用PDO课程[https://github.com/indieteq/PHP-MySQL-PDO-Database-Class/issues/60]

并且有一部分

public function bind($para, $value){
$this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value;
}

之前的值为utf8encode($value)