Home  >  Article  >  Backend Development  >  mysql - PHP randomly inserts a combination of numbers and letters into the database, then determines if it exists in the database, loops it again, and then inserts it into the database.

mysql - PHP randomly inserts a combination of numbers and letters into the database, then determines if it exists in the database, loops it again, and then inserts it into the database.

WBOY
WBOYOriginal
2016-09-19 09:16:291187browse

PHP inserts a random code into the database based on the ID, then determines if the random code already exists in the database and randomly inserts it into the database
mysql - PHP randomly inserts a combination of numbers and letters into the database, then determines if it exists in the database, loops it again, and then inserts it into the database.

Reply content:

PHP inserts a random code into the database based on the ID, then determines if the random code already exists in the database and randomly inserts it into the database
mysql - PHP randomly inserts a combination of numbers and letters into the database, then determines if it exists in the database, loops it again, and then inserts it into the database.

<code>while (true) {
    // 生成随机数
    // 判断数据库中是否存在
    if ($noFount) { // 如果不存在
        // 插入数据库
        break;
    }
}</code>

The field defines a unique key and is inserted using a transaction loop. The insertion is successfully committed and the loop is broken out.

1. First check out all the codes in db
2. Then in_array() until false
3. Insert data

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn