When to use WordPress loop or Foreach loop?

Your examples are showing two different thing The first one is looping over categories and the second is looping over posts, so the answer in this case is simple and depends on what you need to loop over (posts or categories).

No if you meant when should you use query_posts() vs. get_posts() vs. WP_Query
then take a look at this awesome answer .

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)