gordon_shamway
@gordon_shamway

Проблема с кирилицей в PDO?

Почему записи в бд кириллических символов получаю крякозябры?
кодировка Mysql "utf8_general_ci"
class DB
{
    private $username;
    private $password;
    private $servername;
    private $conn;
    private $sql;
    private $table;
    private $column = array();
    private $value=array();
    private $data;
    private $sum; //count data
    private $earray; // count embedded array
    private $string;
    private $manyArray=0;
    /*
    * connect to db
    */
    function __construct($servername,$username,$password)
    {
        $this->servername = $servername;
        $this->username = $username;
        $this->password = $password;
        try {
            $this->conn = new \PDO("mysql:host=$this->servername;dbname=mydatavk", $this->username, $this->password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            $this->conn->exec('SET NAMES utf8');
            // set the PDO error mode to exception
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected successfully"; 
        }catch(PDOException $e){
            echo "Connection failed: " . $e->getMessage();
        }
    }
    
    /*
    * store data
    */
    public function store($table,$data,$column){
        $this->conn->exec('SET NAMES utf8');
      foreach($data as $arr){
            if (is_array($arr)){ 
                $this->manyArray = 1; //check is many array
                break;
            }
        }
        if($this->manyArray==1){
            $this->earray = count($data); //4
            $this->countData = count($data[0]);//12
            for ($i=0; $i <$this->countData;  $i++) {
                $string = null;
                for ($k=0; $k <$this->earray ; $k++) { 
                    $string.="'".$data[$k][$i]."'".",";
                }
                $string = substr($string, 0, -1);
                $this->sql = "INSERT INTO $table($column)VALUES ($string);";
                // $conn->exec($sql);
                echo $this->sql;
                $this->conn->exec($this->sql);
                  
            }
        }else{
            $string=null;
            foreach ($data as $value) {
                $string.="'".$value."'".",";
            }
            $string = substr($string, 0, -1);
            $this->sql = "INSERT INTO $table($column)VALUES ($string)";
            $this->conn->exec($this->sql);
        }
    }
    //code...
}
  • Вопрос задан
  • 562 просмотра
Пригласить эксперта
Ответы на вопрос 1
Steein
@Steein
Программист
$db = new PDO('mysql:host=myhost;dbname=mydb', 'login', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')); 


//Еще один вариант

try
{
    $db = new pdo('mysql:host=localhost;port=3306;dbname=mysql;charset=utf8','user','password',array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    ));
    }catch(PDOException $pe){
        echo $pe->getMessage();
    }
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы