acmumph Posted May 6, 2012 Share Posted May 6, 2012 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 https://forums.phpfreaks.com/topic/262140-how-to-ignore-unreachable-databases/ Share on other sites More sharing options...
scootstah Posted May 6, 2012 Share Posted May 6, 2012 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 https://forums.phpfreaks.com/topic/262140-how-to-ignore-unreachable-databases/#findComment-1343408 Share on other sites More sharing options...
acmumph Posted May 6, 2012 Author Share Posted May 6, 2012 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 https://forums.phpfreaks.com/topic/262140-how-to-ignore-unreachable-databases/#findComment-1343409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.