This way, you creates the button ONLY if it exists in database.
But going even further, maybe you should consider putting the query before the looping, querying all the buttons that exist and than doing the looping for them...