Home  >  Q&A  >  body text

pdo writes garbled characters when the content written to the database is in Chinese

<?php if(empty($_POST['phone'])){ //When this is Chinese, the database is garbled return_json(1,'Mobile phone number cannot be empty'); } if(empty($_POST ['pwd'])){ return_json(2,'Password cannot be empty'); } $phone = find('php_user','*','phone='.$_POST['phone']); if( $phone){ return_json(3,'Mobile phone number already exists'); } $arr = array( 'phone' => $_POST['phone'], 'pwd' => md5($_POST['pwd' ]), 'add_time' => time(), 'nickname' => isset($_POST['nickname']) ? $_POST['nickname'] : $_POST['phone'], 'sex' = > isset($_POST['avatar']) ? $_POST['sex'] : 0, 'avatar' => isset($_POST['avatar']) ? $_POST['avatar'] : '' ); $add = insert('php_user',$arr); return_json(0,'Registration successful'); function connect(){ $db = array( 'charset' => 'utf8', 'port' => 3306, ; 'hao1' ); $dsn = "{$db['type']}:host={$db['host']}; dbname={$db['name']}; charset={$db[ 'charset']}; port={$db['port']}" try { //Instantiate the PDO class and create the PDO object $pdo = new PDO($dsn,$db['user'],$db[ 'pass']); } catch (PDOException $e) { die('Database error:'.$e->getMessage()); } return $pdo;}function insert($table,$data=[]) { //Connect pdo $pdo = connect(); //Create SQL statement $sql = "INSERT INTO {$table} SET "; //Assemble insert statement if(is_array($data)){ foreach ($data as $ k = & gt; $ v) {$ SQL. = $ k. '= "'. $ v. '",';}} else {reTurn false;} // Remove the tail comma and add a segment number to end $ SQL = rtrim(trim($sql),',').';'; //Create PDO preprocessing object $stmt = $pdo->prepare($sql); //Perform new operations if($stmt-> ;execute()){ if($stmt->rowCount()>0){ return true;else {        return false; }}

A快递员***A快递员***1053 days ago852

reply all(3)I'll reply

  • autoload

    autoload2021-11-02 10:01:24

    Look at the encoding method of your database and the encoding method in your php

    reply
    0
  • Cancelreply