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.
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!