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

How to set the character set in php pdo

藏色散人
藏色散人Original
2021-07-14 09:33:192170browse

php pdo method to set the character set: first create a PHP sample file; then connect to the database; 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

How to set the character set for php pdo?

php pdo sets the default character set charset

$dbms = ‘mysql’; //数据库类型
$host = ‘localhost’; //数据库主机名
$dbName = ‘ijishequ’; //使用的数据库
$user = ‘root’; //数据库连接用户名
$pass = ‘’; //对应的密码
d s n = " 
dsn = "
dsn="dbms:host=h o s t ; 
d b n a m e = host;
dbname=host;
dbname=dbName;
charset=utf8";
try {
d b h = n e w P D O ( dbh = new PDO(dbh=newPDO(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!

Statement:
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