Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

php - Suppressing PDO Warnings

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.

Here's my code:

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 }

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:

Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:xampphtdocscdincludesdbconnect.php on line 10

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?

Solution

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! ^^

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) {
...

That will probably solve your problem.

Edit: If the host name is provided by the user, you could validate it before sending it to the PDO constructor.

For example using:

if (filter_var(gethostbyname($user_provided_host_name), FILTER_VALIDATE_IP)) {
  // valid hostname / ip address
}

That will work for domain names, localhost and ip addresses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...