It is narrated from Sayyiduna Umar bin al-Khattab (may Allah be pleased with him) that the Messenger of Allah (peace and blessings be upon him) said: "Actions are judged only by intentions, and a person shall have only what he intended. So whoever's emigration was to Allah and His Messenger, his emigration is to Allah and His Messenger; and whoever's emigration was for worldly gain that he would obtain, or for a woman whom he would marry, his emigration is to that for which he emigrated."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب الطهارة / 181