I want to generate random limited 20 record of sql but not repeat using php code


#1

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

code

$sql = “Select * from record where Limit 20”;
$result = mysqli_query($conn, $sql);
while($myrow = mysqli_fetch_array($result)){
echo “

” . $myrow[“id”] . “ ” . $myrow[“name”] . “<.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


#2

In your code you have told the query to get the first 20 records from the database with the ‘Limit 20’. Remove the limit and you will get all the records or change it to the number of records that you want to display.


#3

Not sure exactly what you need to do but I’m just guessing based on the combination of the subject and the body of your post.

The subject indicates that you want to generate 20 random records. To do that, you would use the LIMIT 20 as you are doing in your code snippet but would also need to throw in something like ORDER BY RAND() to get random values (off the top of my head – refer to MySQL docs for proper use of the ORDER BY / RAND() if that does not work – should not be too hard though).

If the issue is that you are only seeing 20 records but expect to see all of them, then Valkrider is correct, you need to remove the LIMIT 20.

If it is something else that you are aiming for, perhaps you can provide us with some more details.

Good Luck,
Frank


#4

Thanks FrankieRizzo,
Good work i used this code

$sql = “Select * from record where ORDER BY RAND() LIMIT 20”;

thanks all users