我正在尝试连接到数据库,如果有任何问题,我会抛出异常:
class Ex_cnt extends Exception{}
class Ex_slct extends Exception{}
$server = "localhost";
$usr = "root";
$pss = "*******";
$db = "learn";
try{
$cnt = mysqli_connect($server,$usr,$pss);
if(!$cnt){
throw new Ex_cnt("wrong in database details");
}
$dbslct = mysql_select_db($db);
if(!$dbslct){
throw new Ex_slct("wrong database name");
}
}
catch(Ex_cnt $error_cnt){
echo $error_cnt->getMessage();
}
catch(Ex_slct $error_slct){
echo $error_slct->getMessage();
}
问题是此代码显示以下错误
警告:mysqli_connect() [function.mysqli-connect]: (28000/1045): D:\xampp\htdocs\learn\index.php 中用户 'root'@'localhost' 的访问被拒绝(使用密码:YES) 10号线无法连接
我希望它只是显示
无法连接