我想通过它们的唯一 ID 从产品数据库中选择随机行。我每页需要 3-4 个随机产品...
我就是这样开始的
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `products` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `products` WHERE id = $offset LIMIT 1" );
$n=mysql_num_rows($result);
if ($n>0){
while($row = mysql_fetch_array($result)){
$id = $row["id"];
$cod_produs = $row["cod_produs"];
$nume_produs = $row["nume_produs"];
$titlul_paginii = $row["titlul_paginii"];
$nume_intermediar = str_replace (" ", "-", $nume_produs);
$nou_nume_produs = strtolower($nume_intermediar);
$detalii = $row["detalii"];
$seo_descriere = $row["seo_descriere"];
$materiale = $row["materiale"];
$numere = $row["numere"];
$pret = $row["pret"];
$categorie = $row["categorie"];
$subcategorie = $row["subcategorie"];
$data_adaugare = strftime("%b %d, %Y", strtotime($row["data_adaugare"]));
}
echo ("The id of the product is ".$id." and his name is ".$nume_produs);
};
?>
问题是我的代码返回了我随着时间的推移删除的 id ......我如何防止这样做?
以及如何一次返回 3 或 4 个唯一 ID,并愿意接受更好的方法?