Quickest way to check for pre-existing record before insert [mysql_errno()]

后端 未结 2 621
北恋
北恋 2020-12-16 08:31

My question will use emails as an example, but this could apply to anything.


Normally before registering a new user (including inserting his/her email) I check

2条回答
  •  执念已碎
    2020-12-16 09:26

    Use

    INSERT IGNORE INTO Users VALUES(...);
    

    with a unique key on email field, then check row count with mysql_affected_rows();

    This will result in a single query to the DB and rule out the race condition of the time window between SELECT and INSERT

提交回复
热议问题