php pdo method to set the character set: first open the corresponding PHP file; then find "dsn"; finally add "charset=utf8" after dsn.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php pdo sets the default character set charset
$dbms = 'mysql'; //数据库类型 $host = 'localhost'; //数据库主机名 $dbName = 'ijishequ'; //使用的数据库 $user = 'root'; //数据库连接用户名 $pass = 'root'; //对应的密码 $charset = "utf8"; $dsn = "$dbms:host=$host;dbname=$dbName;chartset=$charset"; try { $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象 $result = $dbh->query('SELECT * from goods', PDO::FETCH_ASSOC)->fetchAll(); } catch (PDOException $e) { die("Error!: " . $e->getMessage() . ""); }
Add charset=utf8 after $dsn
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to set the character set in php pdo. For more information, please follow other related articles on the PHP Chinese website!