header("content-type:text/html;charset=utf-8");
//连接数据库服务,并选择数据库
$conn = new mysqli("localhost","root","root","myclass");
//设置字符集
$conn->set_charset("utf8");
//如果连接失败
if($conn->connect_error){
die('链接失败'.$conn->connect_error);
}
try{
$conn=new PDO("mysql:host=127.0.0.1;dbname=myclass;port=3306;charset=utf8","root","root");
}catch (PDOException $e){
echo "出现异常".$e->getMessage();
}
$dsn=数据库类型:host=主机名;dbname=数据库名称;port=端口号;charset=字符集