阿拉伯字符不会被添加到pdo中。
阿拉伯字符不会被添加到pdo中。
我有一个能够很好地处理英文的pdo类,但是当我尝试插入阿拉伯文本时,它的效果不好。
这是这个类:
class DB extends config { protected $pdo; private $sQuery; private $bConnected = false; private $log; private $parameters; protected $table; protected $values; public function __construct($table) { parent::__construct(); $this->log = new Log(); $this->Connect(); $this->parameters = array(); $this->table = $table; } private function Connect() { $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"]; try { $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->bConnected = true; } catch (PDOException $e) { echo $this->ExceptionLog($e->getMessage()); die(); } } public function CloseConnection() { $this->pdo = null; } private function Init($query,$parameters = "") { if(!$this->bConnected) { $this->Connect(); } try { $this->sQuery = $this->pdo->prepare($query); $this->bindMore($parameters); if(!empty($this->parameters)) { foreach($this->parameters as $param) { $parameters = explode("\x7F",$param); $this->sQuery->bindParam($parameters[0],$parameters[1]); } } $this->succes = $this->sQuery->execute(); } catch(PDOException $e) { echo $this->ExceptionLog($e->getMessage(), $query ); die(); } $this->parameters = array(); } public function bind($para, $value) { $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . utf8_encode($value); } public function bindMore($parray) { if(empty($this->parameters) && is_array($parray)) { $columns = array_keys($parray); foreach($columns as $i => &$column) { $this->bind($column, $parray[$column]); } } } public function query($query,$params = null, $fetchmode = PDO::FETCH_ASSOC) { $query = trim($query); $this->Init($query,$params); $rawStatement = explode(" ", $query); $statement = strtolower($rawStatement[0]); if ($statement === 'select' || $statement === 'show') { return $this->sQuery->fetchAll($fetchmode); } elseif ( $statement === 'insert' || $statement === 'update' || $statement === 'delete' ) { return $this->sQuery->rowCount(); } else { return NULL; } } public function lastInsertId() { return $this->pdo->lastInsertId(); } public function column($query,$params = null) { $this->Init($query,$params); $Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM); $column = null; foreach($Columns as $cells) { $column[] = $cells[0]; } return $column; } public function row($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) { $this->Init($query,$params); return $this->sQuery->fetch($fetchmode); } public function single($query,$params = null) { $this->Init($query,$params); return $this->sQuery->fetchColumn(); } private function ExceptionLog($message , $sql = "") { $exception = 'Unhandled Exception. '; $exception .= $message; $exception .= " You can find the error back in the log."; if(!empty($sql)) { $message .= "\r\nRaw SQL : " . $sql; } $this->log->write($message); return $exception; } public function binding($values = array()){ foreach($values as $key => $value){ $this->bind($key,$value); $this->values[$key] = $value; } } public function add($where = NULL){ $sql = "INSERT INTO {$this->table} ("; $i=0; foreach($this->values as $key => $value){ if($i+1 == count($this->values)) { $sql.= $key; } else { $sql.= $key.','; } $i++; } $sql .= ') values ('; $i=0; foreach($this->values as $key => $value){ if($i+1 == count($this->values)) { $sql.= ":{$key}"; } else { $sql.= ":{$key},"; } $i++; } $sql .= ')'; if(!empty($where)){ $sql+=$where; } $query = $this->query($sql); return $query; } public function delete($id){ $sql = "DELETE FROM {$this->table} WHERE id = :id"; $this->bind("id",$id); $query = $this->query($sql); return $query; } public function update($where){ $sql= "UPDATE {$this->table} SET"; $i=0; foreach($this->values as $key => $value){ if($i == count($this->values)) { $sql.= "{$key} = ':{$key}'"; } else { $sql.= "{$key} = ':{$key}',"; } $i++; } $sql .= $where; $query = $this->query($sql); return $query; } }
当我这样使用它时:
include_once './includes/int.php'; $db = new Db("test"); $db->binding(array( "test" => "ابلابا" )); $add = $db->add(); print_r( $db->row("select * from test where id = 5"));
它给我这个结果:
Array ( [id] => 5 [test] => ابÙابا )
我在phpMyadmin中设置了列的字符集为utf8mb4_unicode_ci。
编辑:
我的表的字符集是utf8_unicode_ci。
编辑2:
我已经检查了这个问题https://stackoverflow.com/questions/279170/utf-8-all-the-way-through,但我已经都完成了。
编辑3:
title binding(array( "test" => "ابلابا" )); $add = $db->add(); print_r( $db->row("select * from test where id = 12")); ?>
仍然不起作用。
在您的 Connect 函数中添加以下代码:
$this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $this->pdo->exec("SET CHARACTER SET utf8"); // <-- HERE
并确保在您的 HTML 页面中设置正确的编码字符集:
在 bind 函数中删除 utf8_encode:
public function bind($para, $value){ $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value; }
您使用的是哪个 IDE!
您的 IDE 是否使用 UTF8:[how to change default encoding in netbeans 8 0](http://stackoverflow.com/questions/24778725)
嗯,您可以创建一个新的具有 UTF8 编码的文件,并将插入代码放入其中,一个简单的插入查询。
我找到了问题的来源,是 utf8_encode 函数,您应该将其删除。