The Messenger of Allah (ﷺ) said: “Whoever sees a person afflicted and say: ‘All praise is due to Allah Who saved me from that which He has afflicted you with, and blessed me greatly over many of those whom He has created, (Al-ḥamdulillāhi alladhī `āfānī mimmabtalāka bihī wa faḍḍalanī `alā kathīrin mimman khalaqa tafḍīla)’ then he shall be saved from that affliction for as long as he lives.”
Jami` at-Tirmidhi Book 48, Hadith 62