我有一个简单的类,应该将我的值插入 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
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
答案 0 :(得分:0)
您应该为表格设置UTF-8字符集。
Database Chartset = utf8 & Database Collation = utf8_general_ci .
你应该永久地为你的桌子做这件事
那应该有用!检查以下链接是否有帮助: