This question already has answers here:
Turning query errors to Exceptions in MySQLi [duplicate]
(3个答案)
1年前关闭。
我碰到了这个:
PHP Error handling: die() Vs trigger_error() Vs throw Exception
并理解抛出异常更好
我如何在此代码中替换die并在这里使用throw异常:-
(3个答案)
1年前关闭。
我碰到了这个:
PHP Error handling: die() Vs trigger_error() Vs throw Exception
并理解抛出异常更好
我如何在此代码中替换die并在这里使用throw异常:-
<?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();
}
关于php - 如何在mysql数据库连接中使用抛出异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9836693/