break out of loop when meet condition then continue loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wilsonee
Forum Newbie
Posts: 7
Joined: Fri Jun 06, 2008 10:42 pm

break out of loop when meet condition then continue loop

Post by wilsonee »

Hi,

I'm beginner in PHP.
Below is how I connect to multiple databases to query some status info.
Problem I have is, if the database is down, the code prints "Could not connect ..." and exits without connecting to rest of databases in DatabaseList. I'm not sure of a way to continue querying the rest of databases that are up.
What can I do to achieve this?

for ($row = 0; $row < $result_c; $row++)
{
$connection = mysql_connect($DatabaseList[$row]["db_hostname"], $DatabaseList[$row]["db_username"], $DatabaseList[$row]["db_password"]);
if (!$connection)
{
die ("Could not connect to database:".$DatabaseList[$row]["db_hostname"]."<br />". mysql_error());
} else {
//do something
}
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: break out of loop when meet condition then continue loop

Post by cpetercarter »

"die" kills the process.Try replacing it with "echo".
wilsonee
Forum Newbie
Posts: 7
Joined: Fri Jun 06, 2008 10:42 pm

Re: break out of loop when meet condition then continue loop

Post by wilsonee »

Thanks cpetercarter.

:oops: I should understand the code before copying and pasting.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: break out of loop when meet condition then continue loop

Post by jackpf »

You should. You could copy and paste some code that wipes your entire server. That'd be shameful.
Post Reply