Jump to content

How to ignore unreachable databases?


acmumph

Recommended Posts

Below is some code I have that I would like to connect to databases that reside on different physical machines. The problem I can't seem to solve is how to ignore machines that are unreachable.

<html>
<head>

</head>

<body>
<?php
$mysql_conn=new mysqli('192.168.1.67','admin','password','monkey');
$result=$mysql_conn->query("SELECT * from testtable");
	echo "<table>";
	echo "<th>Test Query</th>";
while ($row=$result->fetch_array(MYSQL_ASSOC)){
echo "<tr><td>";
echo $row['test1'];
echo "</td></tr>";
}
echo "</table>";

$mysql_conn=new mysqli('192.168.1.126','admin','','monkey1');
$result=$mysql_conn->query("SELECT * from demo");
	echo "<table>";
	echo "<th>Test Processor</th>";
while ($row=$result->fetch_array(MYSQL_ASSOC)){
echo "<tr><td>";
echo $row['name'];
echo "</td></tr>";
}
echo "</table>";
$mysql_conn=new mysqli('192.168.1.127','admin','','monkey2');
$result=$mysql_conn->query("SELECT * from demo");
	echo "<table>";
	echo "<th>Test Processor</th>";
while ($row=$result->fetch_array(MYSQL_ASSOC)){
echo "<tr><td>";
echo $row['processor'];
echo "</td></tr>";
}
echo "</table>";
?>

</body>
</html>

 

If 192.168.1.126 is offline, the webpage doesn't go any further and display 192.168.1.127's info...Appreciate any insight...

 

 

Link to comment
Share on other sites

You could check for connection errors each time.

$mysql_conn=new mysqli('192.168.1.67','admin','password','monkey');

if (!$mysql->connect_error) {
$result=$mysql_conn->query("SELECT * from testtable");
	echo "<table>";
	echo "<th>Test Query</th>";
while ($row=$result->fetch_array(MYSQL_ASSOC)){
echo "<tr><td>";
echo $row['test1'];
echo "</td></tr>";
}
echo "</table>";
}

 

There might be a better solution, but I don't really know what you're trying to achieve.

Link to comment
Share on other sites

Worked like a champ...There are times that certain machines I am responsible for are off-line and my webpage would hang up at the point a mysql connection was trying to be made to that off-line box. Once it was brought back on-line and page refreshed it would continue displaying rest of page...Your solution allowed me to by-pass the mysql connection/query that was unreachable....

 

Thanks for the help...

 

<html>
<head>

</head>

<body>
<?php
$mysql_conn=new mysqli('192.168.1.68','admin','password','monkey');
if (!$mysql_conn->connect_error) {	
$result=$mysql_conn->query("SELECT * from testtable");
	echo "<table>";
	echo "<th>Test Query</th>";
while ($row=$result->fetch_array(MYSQL_ASSOC)){
echo "<tr><td>";
echo $row['test1'];
echo "</td></tr>";
}
echo "</table>";
}
$mysql_conn=new mysqli('192.168.1.126','admin','','monkey1');
if (!$mysql_conn->connect_error) {	
$result=$mysql_conn->query("SELECT * from demo");
	echo "<table>";
	echo "<th>Test Processor</th>";
while ($row=$result->fetch_array(MYSQL_ASSOC)){
echo "<tr><td>";
echo $row['name'];
echo "</td></tr>";
}
echo "</table>";
}
?>


</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.