我想用PHP编写一个具有数据库连接和异常处理的程序。如果我插入一个不正确的用户名,那么它应该显示它相应的错误消息,如果我插入了不正确的数据库,它应该显示它相应的错误消息。
但在下面的程序中,无论我插入了不正确的数据库还是不正确的用户名,它只显示消息“无法连接到数据库”。
<?php
$hostname = "localhost";
$username = "root1";
$password = "";
$database = "php_thenewboston";
$conn = mysqli_connect($hostname,$username,$password);
$conn_db = mysqli_select_db($conn,$database);
class ServerException extends Exception{}
class DatabaseException extends Exception{}
try{
if(!$conn){
throw new ServerException('Could not connect to server.');
}elseif(!$conn_db){
throw new DatabaseException('Could not connect to database.');
}else{
echo "Connected.";
}
}catch(ServerException $ex){
echo "Error :".$ex->getMessage();
}catch(DatabaseException $ex){
echo "Error :".$ex->getMessage();
}
?>我是PHP的初学者。如有任何疑问,请在下面评论。
编辑
下面的@Hatef询问用户名不正确、密码正确和数据库名正确时是var_dump of $conn
object(mysqli)#1 (19) {
["affected_rows"]=>
int(-1)
["client_info"]=>
string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $"
["client_version"]=>
int(50012)
["connect_errno"]=>
int(0)
["connect_error"]=>
NULL
["errno"]=>
int(1044)
["error"]=>
string(68) "Access denied for user ''@'localhost' to database 'php_thenewboston'"
["error_list"]=>
array(1) {
[0]=>
array(3) {
["errno"]=>
int(1044)
["sqlstate"]=>
string(5) "42000"
["error"]=>
string(68) "Access denied for user ''@'localhost' to database 'php_thenewboston'"
}
}
["field_count"]=>
int(0)
["host_info"]=>
string(20) "localhost via TCP/IP"
["info"]=>
NULL
["insert_id"]=>
int(0)
["server_info"]=>
string(21) "5.5.5-10.1.16-MariaDB"
["server_version"]=>
int(50505)
["stat"]=>
string(132) "Uptime: 1072 Threads: 1 Questions: 16 Slow queries: 0 Opens: 18 Flush tables: 1 Open tables: 11 Queries per second avg: 0.014"
["sqlstate"]=>
string(5) "00000"
["protocol_version"]=>
int(10)
["thread_id"]=>
int(9)
["warning_count"]=>
int(0)
}下面是用户名正确、密码正确和数据库名不正确时的var_dump of $conn。
object(mysqli)#1 (19) {
["affected_rows"]=>
int(-1)
["client_info"]=>
string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $"
["client_version"]=>
int(50012)
["connect_errno"]=>
int(0)
["connect_error"]=>
NULL
["errno"]=>
int(1049)
["error"]=>
string(36) "Unknown database 'php_thenewboston1'"
["error_list"]=>
array(1) {
[0]=>
array(3) {
["errno"]=>
int(1049)
["sqlstate"]=>
string(5) "42000"
["error"]=>
string(36) "Unknown database 'php_thenewboston1'"
}
}
["field_count"]=>
int(0)
["host_info"]=>
string(20) "localhost via TCP/IP"
["info"]=>
NULL
["insert_id"]=>
int(0)
["server_info"]=>
string(21) "5.5.5-10.1.16-MariaDB"
["server_version"]=>
int(50505)
["stat"]=>
string(132) "Uptime: 1417 Threads: 1 Questions: 18 Slow queries: 0 Opens: 18 Flush tables: 1 Open tables: 11 Queries per second avg: 0.012"
["sqlstate"]=>
string(5) "00000"
["protocol_version"]=>
int(10)
["thread_id"]=>
int(10)
["warning_count"]=>
int(0)
}发布于 2017-05-03 18:04:51
默认情况下,mysql会自动抛出异常。要手动抛出异常,您需要在文件顶部写入下面的行--即使在mysqli_connect上也会抛出异常。因此,您需要在try块中添加connect方法。mysqli_report(MYSQLI_REPORT_STRICT);
更新:在您的情况下,不要编写上面的行,更改主机名&您将在catch块中得到服务器错误。
发布于 2017-05-13 08:27:56
按照您的代码,您听起来是个不错的oops程序员。好吧,每种编程语言都有自己的规则和标准。
让我们检查一下您的代码
$conn = mysqli_connect($hostname,$username,$password);
$conn_db = mysqli_select_db($conn,$database);
class ServerException extends Exception{}
class DatabaseException extends Exception{}在这里,您正在尝试启动一个连接。然后,在尝试捕获块中,您正在检查连接性。
将您的连接也放在try catch块中&捕获异常。
例如:
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();
}发布于 2017-05-17 06:39:11
使用mysqli_connect,您可以在同一个函数中传递数据库名,因此为了连接目的,不需要等待mysqli_select_db()
有许多错误代码,您将获得后,连接尝试,并在此基础上,您将能够指定适当的消息。
我已经更新了你的代码和条件,这样你就知道了。
<?php
$hostname = "localhost";
$username = "root1";
$password = "";
$database = "php_thenewboston";
$conn = mysqli_connect($hostname,$username,$password,$database);
$error = mysqli_connect_errno();
//$conn_db = mysqli_select_db($conn,$database);
class ServerException extends Exception{}
class DatabaseException extends Exception{}
try{
if($error == 1044){
throw new ServerException('Could not connect to server. Check Username');
}elseif($error == 1045){
throw new DatabaseException('Could not connect to server. Check Password');
}elseif($error == 1049){
throw new DatabaseException('Could not connect to database. Check Database Name');
}else{
echo "Connected.";
}
}catch(ServerException $ex){
echo "Error :".$ex->getMessage();
}catch(DatabaseException $ex){
echo "Error :".$ex->getMessage();
}
?>希望它能帮到你。
https://stackoverflow.com/questions/43766979
复制相似问题