【发布时间】:2017-08-02 22:27:26
【问题描述】:
我有一个简单的类,应该将我的值插入到我的 mysql 表中。
The Tables will make by administrator and table names would be in Persian,例如:
DBNAME===>aalborz
TableName==>سارا
这是我的课程:
<?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...???
我检查了这些链接和问题的答案,但它们不是我的答案:
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
@ 987654325@
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
【问题讨论】:
-
我正在检查,感谢您的警告,如果这是我的问题,我将删除我的帖子...
-
我不完全确定,因为我从未在表名或列名中使用过 utf8 字符,但它是一个非常完整的指南/清单。
-
我从 10 小时前开始尝试,我已经测试了很多不同的方法,我很困惑......这个问题也没有帮助,但再次感谢你
标签: php mysql database character-encoding