I have a loop where the code loops and in every loop it connects to different server and then makes MySQL query. However sometimes one of the loops/queries cannot reach to the end and all the code breaks. Is there a way where if the loop gives error then skip the processes / move to next loop?
As stated in the comments above, the correct way to do this is within a try...catch
block.
To learn more about these, you can read about them here.
Thanks @Paul.
Inside that loop ,check the connection and if it is made, do the logic. So that if connection is not made, the logic part would be skipped.
Use "continue" to skip to the next item in hte loop:
This all depends on if you use php's built-in mysql, mysqli or some third party library. But as a general rule I would recommend the following logic.
Depending on the work you do on the table in question you could also suppress any errors from your query using the @ prefix like @mysql_query and then check for number of affected rows. If you do a select you could check your result set to see if you got anything back etc. I don't like the use of @ though and it should be noted that a select from an existing table that was empty and similar situations could result in strange behavior depending on your code. Try/Catch is a nicer approach to handle error control flow. But actually throwing an exception is quite a slow process in comparison and since all the work revolves around connecting to databases all over the place it might not be a good idea to slow it down even more if a lot of exceptions could be thrown by this loop. Also, when using it to skip a step in a loop it can be quite hard for some people to follow the logic. Therefore something like @ could be acceptable here ;)