-1

我想让每个数据都存在于数据库中,然后循环获取唯一数据。

这是我的代码:

$id = rand(10000000,99999999);
$check_id = $db->prepare("SELECT * FROM sh_url WHERE sh_id='$id'");
$check_id->execute();
$count_id = $check_id->rowCount();

for ($count_id != 0) {
    $lid = $id+1;
}

$shorturl = htmlentities(base_convert($lid,20,36));

$query = $db->prepare("INSERT INTO `sh_url`(`sh_id`) VALUES (:id)");
$query->bindParam(":id", $lid);
$query->execute();
4

1 回答 1

0

你为什么不使用 distinct 关键字来做到这一点。

SELECT DISTINCT column1, column2, ...
FROM table_name;
于 2018-04-28T04:00:42.250 回答