$type = 'mysql'; //数据库类型 $host = '127.0.0.1'; //主机名 $dbname = 'php_edu'; //数据库名 $charset = 'utf8'; //默认编码 $dsn = $type.':host='.$host.';dbname='.$dbname.';charset='.$charset; $user = 'root'; $pass = 'root'; try { //连接 $pdo = new PDO($dsn, $user, $pass); } catch (PDOException $e) { exit($e->getMessage()); } finally { //关闭 $pdo = null; }