Home > Backend Development > PHP Problem > How to set the character set in php pdo

How to set the character set in php pdo

藏色散人
Release: 2023-03-09 14:54:02
Original
3127 people have browsed it

php pdo method to set the character set: first open the corresponding PHP file; then find "dsn"; finally add "charset=utf8" after dsn.

How to set the character set in php pdo

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() . "");
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template