You don't need a different query to check if the record with mid = .. already exists. You can simply handle this with the mysql ON DUPLICATE KEY UPDATE statement which will do this for you in only 1 query!