如何在mysql数据库连接中使用throw exception [英] how to use throw exception in mysql database connect
本文介绍了如何在mysql数据库连接中使用throw exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到这个问题: -
I come across this:-
PHP错误处理:die()Vs trigger_error()Vs throw Exception
是更好的
如何替换die和use throw exception在这里代码:
How can i replace die and use throw exception here in this code:-
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_db = "localhost";
$database_db = "database";
$username_db = "root";
$password_db = "password";
$db = mysqli_connect($hostname_db, $username_db, $password_db) or die("Unable to connect with Database");
?>
推荐答案
try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
//do something
}
else
{
throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
这篇关于如何在mysql数据库连接中使用throw exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文