duanpenpan5796 2018-06-19 05:33
浏览 94

我想生成随机有限的20条sql记录,但不能重复使用PHP代码

I make website using php code and mysql.
total record in database 77.
i see only 20 record of database using php.
i using this code

$sql = "Select * from record where Limit 20";
$result = mysqli_query($conn, $sql);
while($myrow = mysqli_fetch_array($result)) {
    echo "<tr><td>" . $myrow["id"] . "</td><td>" . $myrow["name"] . "</td></tr>";
}  

record show is

ID         Name
1          ali1
2          ali2
3          ali3
........
20         ali20

but i see record random and not repeat any record i show record this

ID         Name
3          ali3
55         ali55
22         ali22
60         ali60

how can this

  • 写回答

1条回答 默认 最新

  • down123321123 2018-06-19 05:44
    关注

    Try this one to get random 20 records from table:

    SELECT * FROM table_name
    ORDER BY RAND()
    LIMIT 20;
    
    评论

报告相同问题?