在我的所有PHP页面中,我都包含以下数据库连接代码
<?PHP
$dbInfo = parse_ini_file('path/to/ini/file');
define ("HOST", $dbInfo["HOST"]); // The host you want to connect to.
define("USER", $dbInfo["USER"]); // The database username.
define("PASSWORD", $dbInfo["PASSWORD"]); // The database password.
define("DATABASE", $dbInfo["DATABASE"]); // The database name.
$MysqLi = new MysqLi(HOST, USER, PASSWORD, DATABASE);
// If you are connecting via TCP/IP rather than a UNIX socket remember to add the port number as a parameter.
if (MysqLi_connect_error()) {
die('fail');
} else {
echo 'success';
}
If ((USER == "USERNAME HERE") || (PASSWORD == "PASSWORD HERE")){
print 'ERROR - Please set up the script first';
exit();
}
?>
无论我做什么(例如输入错误的密码或将文件的路径设置错误),它总是回应“成功”.我尝试使用try / catch来处理错误,使用面向对象的If($MysqLi-> connect_error)将@放在新的MysqLi前面.我所做的一切都不会失败
当我放
echo 'Success... ' . $MysqLi->host_info . "n";
我得到了,而不是回声成功
Success... Localhost via UNIX socket
任何人都可以告诉我为什么我一直得到这个虚假的成功信息? 编辑:我最终寻找的不是每个PHP错误消息,例如我用于开发(即ini_set(‘display_errors’,1);),但只是我放入的单个“失败”消息如果是connection_error语句.@H_404_28@
Edit2:var_dump的结果($MysqLi);使用错误的文件路径(parse_ini_file指向没有文件的位置)
object(MysqLi)#1 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.5.32" ["client_version"]=> int(50532) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(23) "5.5.32-0ubuntu0.12.10.1" ["server_version"]=> int(50532) ["stat"]=> string(135) "Uptime: 8944 Threads: 1 Questions: 2445 Slow queries: 0 Opens: 275 Flush tables: 1 Open tables: 67 Queries per second avg: 0.273" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(468) ["warning_count"]=> int(0) }
和var_dump的结果(MysqLi_connect_error());
NULL
var_dump($MysqLi)的结果;使用正确的文件路径(经过测试和验证)
object(MysqLi)#1 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.5.32" ["client_version"]=> int(50532) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(23) "5.5.32-0ubuntu0.12.10.1" ["server_version"]=> int(50532) ["stat"]=> string(136) "Uptime: 15306 Threads: 1 Questions: 2446 Slow queries: 0 Opens: 275 Flush tables: 1 Open tables: 67 Queries per second avg: 0.159" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(471) ["warning_count"]=> int(0) }
NULL
正如您所看到的,两个var_dumps的结果是相同的(正常运行时间除外),无论我是否在parse_ini_file中输入了正确的路径,或者我是否将其指向没有文件的虚假位置.
编辑3:问题已得到解答,请在评论中查看,直到有足够的时间让我回答我自己的问题. 解决方法: 感谢用户“你的常识”,指出我正确的方向.
根据MysqLi构造http://php.net/manual/en/mysqli.construct.php上的文档,当host为NULL或未定义时,默认为localhost,当密码为NULL或未定义时
“the MysqL server will attempt to authenticate the user against those user records which have no password only.”
因此,每当我指向虚假文件位置时,它都会尝试使用任何不接受密码的用户名连接到localhost.不幸的是,默认情况下,MysqL安装三个具有全局访问权限且没有密码的用户(仅限使用权限).导致我的伪造文件位置获得成功连接.
解决方案是删除这三个用户(和测试数据库).我现在使用任何数量的前面提到的方法失败,包括
if (NULL !== MysqLi_connect_error()) {
die('fail');
} else {
echo 'success';
}
要么
if (MysqLi_connect_error()) {
die('fail');
} else {
echo 'success';
}
要么
if ($MysqLi->connect_error) {
die('Connect Error (' . $MysqLi->connect_errno . ') '. $MysqLi->connect_error);
}
(编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|