The Messenger of Allah ﷺ said, "Whoever sees someone afflicted and says, 'Praise be to Allah who has spared me from what He has tested you with and has favored me greatly over many of those He has created,' will not be struck by that affliction."
حدثنا أبو جعفر السمناني وغير واحد قالوا حدثنا مطرف بن عبد الله المديني حدثنا عبد الله بن عمر العمري عن سهيل بن أبي صالح عن أبيه
قال رسول الله ﷺ من رأى مبتلى فقال الحمد لله الذي عافاني مما ابتلاك به وفضلني على كثير ممن خلق تفضيلا لم يصبه ذلك البلاء
Its isnad is daif.It was also narrated by Tabarani in al-Awsat, no. 4721, from Mutarrif through him, and he said, "Only Abdullah bin Umar Umari transmitted it alone."Abdullah bin Umar Umari is daif.The hadith has corroborating narrations found in Abu Naeem's al-Hilyah, 5/13, and elsewhere.