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==>سارا
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