lundi 24 octobre 2016

How select random column value from Mysql database using PHP

I'm trying to fetch random column from database using Rand() function. It's returning random value but many time it's returning duplicate. This is what my database table look like.

|------
|Column|Type|Null|Default
|------
|no|int(30)|No|
|postid|varchar(100)|Yes|NULL
|byuser|varchar(32)|Yes|NULL
|likeslimit|int(30)|No|
|createdon|date|No|
|-----

And this is what my PHP code is.

$query = mysqli_query($mysql, "SELECT postid FROM history ORDER BY Rand() Limit 1");
    if (mysqli_num_rows($query) == 1) {
        while ($row = mysqli_fetch_assoc($query)) {
            echo $row['postid'];
        }
    }

I want it to always return random never the same till the end of data reached.




Aucun commentaire:

Enregistrer un commentaire