How do I select random rows (say 10 from 100) from mysql database and display them with a number in series
Any random rows get selected and give them like
1. XXXXX
2. YYYYY
3. ZZZZZ
but in database they are not in series
How do I select random rows (say 10 from 100) from mysql database and display them with a number in series
Any random rows get selected and give them like
1. XXXXX
2. YYYYY
3. ZZZZZ
but in database they are not in series
You could use the PHP rand()
function. Here's the tutorial for it : https://www.w3schools.com/php/func_math_rand.asp
Also to gave them an ordered list, use <ol>
.
Example
$iWantThisManyRow = 4;
$minNumber = 10;
$maxNumber = 100;
for ($i=0; $i < $iWantThisManyRow; $i++) {
$rand = rand($minNumber, $maxNumber);
$sql = "SELECT * FROM `tables` WHERE `id` = '$rand'";
$result = mysqli_query($link, $sql);
if(mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
$data = $data['field'];
echo "<ol>$data</ol";
}else{
// Whatever you wanted to do when nothing was founded.
}
}
Please note that you might want to use prepared statement and the example above is assuming $link
is the database connection and field
is the column name.
` https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol – brombeer Oct 04 '18 at 10:36