我在文件index.php中有以下代码:
include('db_connect.php'); //Here's the script that provides mysql connection to a database
$a = round(rand(1,100));
$b = round(rand(160,202));
$c = round(rand(50,110));
$d = round(rand(1,99999));
$sql = "INSERT INTO sInstance (p1,p2,p3,p4) VALUES ('".$a."','".$b."','".$c."','".$d."')";
$result = mysql_query($sql) or die(mysql_error());
显然,它随机生成值并将它们作为记录插入到 MyISAM 数据库表sInstance中。
问题:在生成和插入约 1000 条记录后,几乎不可能生成新记录(具有唯一值集)。每个新记录都是一些旧记录的克隆(具有相同的 p1-p2-p3-p4 值)。
问题:如何避免这种情况并使php生成唯一记录?
线索:这是 PHP 的服务器端问题,可能它以某种方式缓存了在 RAND() 函数的帮助下设置的值。问题既没有连接到浏览器,也没有连接到 mysql 本身。