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

How to set the character set in php pdo

藏色散人
藏色散人 Original
2021-05-10 10:28:46 2940browse

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

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