Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

PHPPDO如果连接MySQL失败,如何返回一些信息?

PHPPDO如果连接MySQL失败,如何返回一些信息?


当连接失败(数据库出问题时),PDO的new PDO连接将输出一个“Sorry MySQL”或者向浏览器输出一个警告弹窗

想要这样  怎么写?

新手问题,感谢各位老师解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

其实这样就可以了

try{
    $pdo = new PDO("mysql:host=localhost;dbname=dbname","root","");
}catch(PDOException $e){
    die("数据库连接失败");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...