我将使用 NFC 的 GET 方法来提取一些数据,我想用大量唯一的数字填充数据库,我不介意其中包含关键字,例如
TOM05543
TOM04423
KEL04432
KAL43242
执行此操作并事先用这些唯一 ID 填充我的数据库的最佳方法是什么?
最佳答案
创建一个 PHP 函数,它可以生成随机的 apha 数字代码并检查数据库表中是否存在该代码。如果存在,则递归调用函数,直到生成唯一的代码。并在数据库表中插入唯一生成的记录。
<?php
//DB connection
$con=mysqli_connect("localhost","my_user","my_password","my_db");
//Function to generate unique alpha numeric code
function generateRandomNumer($con,$len=8){
$randomString = substr(MD5(time()),$len);
//Check newly generated Code exist in DB table or not.
$query = "select * from table_name where col_name='".$randomString."'";
$result=mysqli_query($con,$query);
$resultCount=mysqli_num_rows($result);
if($resultCount>0){
//IF code is already exist then function will call it self until unique code has been generated and inserted in Db.
generateRandomNumer($con);
}else{
//Unique generated code will be inserted in DB.
mysqli_query($con,"INSERT INTO Persons (col_name) VALUES ('".$randomString."')");
}
}
//Loop to insert number of unique code in DB.
//NUM_OF_RECORD_YOU_WANT_TO_INSERT define constant which contain number of unique codes you wants to insert in DB.
for($i=0;$i<NUM_OF_RECORD_YOU_WANT_TO_INSERT;$i++){
generateRandomNumer($con);
}
?>
关于php - SQL 和 PHP 唯一编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39346669/