<?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; ?>