抑制PDO警告 [英] Suppressing PDO Warnings
问题描述
我正在尝试根据数据库是否可以连接来显示服务器状态。与老学校 mysql_connect()
和 mysqli_connect()
很容易。我正在努力保持现代化,所以我使用PDO,但是我不知道如何禁止默认警告。从我可以看出,您需要使用 getMessage()
函数来打印PDO警告,但我没有使用它。
I'm trying to display a server status, based upon whether the database can be connected to or not. With the old school mysql_connect()
and mysqli_connect()
it was easy. I'm trying to stay modern, so I'm using PDO, but I can't figure out how-to suppress the default warning. From what I can tell, you need to use the getMessage()
function for it to print the PDO warning, but I'm not using it.
这是我的代码:
8 $dbstatus = 1;
9 try {
10 $db = new PDO($dbms . ':host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname, $dbuser, $dbpasswd);
11 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
12 } catch(PDOException $e) {
13 $dbstatus = 0;
14 }
15 if($dbstatus == 1) {
16 echo '<span style="color: green">DB Up</span>';
17 } else {
18 echo '<span style="color: red">DB Down</span>';
19 exit;
20 }
所有连接变量都提供正确,但 $ dbhost
,这是故意破碎来测试这个。现在,它会产生所需的结果,但也会打印警告信息:
All the connection variables are supplied and correct, except the $dbhost
, which is intentionally broken to test this. Now, it produces the desired results, but is also prints a warning message too:
警告:PDO: :__ construct():php_network_getaddresses:getaddrinfo failed:没有这样的主机是已知的。在 C:\xampp\htdocs\cd\includes\dbconnect.php 行 10
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\cd\includes\dbconnect.php on line 10
如果我更正 $ dbhost
变量,它工作正常,所以我知道PDO语句不可用的问题。
If I correct the $dbhost
variable, it works fine, so I know the issue isn't with the PDO statement being usable.
任何关于我失踪的想法?
Any ideas on what I'm missing?
解决方案
我使用了jeroen提供的变体:
I used a variation of what was supplied by jeroen:
if(filter_var(gethostbyname($dbhost), FILTER_VALIDATE_IP)) {
$dbstatus = 1;
try {
$db = new PDO($dbms . ':host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname, $dbuser, $dbpasswd, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch(PDOException $e) {
$dbstatus = 0;
}
} else {
$dbstatus = 0;
}
if($dbstatus == 1) {
echo '<span style="color: green">DB Up</span>';
} else {
echo '<span style="color: red">DB Down</span>';
exit;
}
感谢您的帮助,我希望这有助于别人! ^^
Thank you for the help and I hope this helps someone else! ^^
推荐答案
我唯一可以看到的是,你告诉PDO在您尝试打开连接。这很可能太晚了。
The only thing I can see here, is that you tell PDO to throw exceptions after you have tried to open the connection. That is most likely too late.
您可以做什么,直接使用第四个参数将该选项发送给构造函数:
What you could do instead, is send that option to the constructor directly using the 4th parameter:
try {
$opts = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$db = new PDO($dbms . ':host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname,
$dbuser, $dbpasswd, $opts);
} catch(PDOException $e) {
...
可能会解决您的问题。
编辑:如果用户提供主机名,可以在将其发送到PDO构造函数之前进行验证
If the host name is provided by the user, you could validate it before sending it to the PDO constructor.
例如使用
if (filter_var(gethostbyname($user_provided_host_name), FILTER_VALIDATE_IP)) {
// valid hostname / ip address
}
这将适用于域名 localhost
和IP地址。
That will work for domain names, localhost
and ip addresses.
这篇关于抑制PDO警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!