PDO创建mysql数据库并指定utf8编码
<?php
//PDO创建mysql数据库并指定utf8编码 header('Content-type:text/html; charset=utf-8'); $servername = "localhost"; $username = "root"; $password = ""; $dsn="mysql:host=$servername"; try { $pdo = new PDO($dsn, $username, $password); // 设置 PDO 错误模式为异常 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "CREATE DATABASE IF NOT EXISTS MY_DB DEFAULT CHARSET utf8 COLLATE utf8_general_ci"; $result=$pdo->prepare($sql)->execute(); echo "数据库创建成功<br>"; } catch(PDOException $e) { echo "<br>" . $e->getMessage(); } $pdo = null; ?>