0

I'm trying to fetch random column from database using Rand() function. It's returning random value but many time it is 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.

2
  • put the selection in array and check if in_array and ignore it Commented Oct 24, 2016 at 7:43
  • Can you please tell me how? Commented Oct 24, 2016 at 7:48

3 Answers 3

2

Don't use loop and condition you want only 1 limit try this

$query = mysqli_query(
    $mysql,
    "SELECT postid FROM history ORDER BY Rand() LIMIT 1"
);

$row = mysqli_fetch_assoc($query)

echo $row['postid'];
     
Sign up to request clarification or add additional context in comments.

1 Comment

Still duplicates.
1

This is the way RAND in mysql works and will repeat the results from time to time. But you can achieve such functionality by using mysql with php.

$query = mysqli_query($mysql, "SELECT postid FROM cacheTable WHERE 1 ORDER BY RAND() LIMIT 1");
$row = mysqli_fetch_assoc($query);
$foundId = (int)$row['postid'];

if((int) $foundId === 0) { // NO records left in cacheTable then fill it up again
    mysqli_query($mysql, "INSERT INTO cacheTable (postid) SELECT postid FROM history");
    $query = mysqli_query($mysql, "SELECT postid FROM cacheTable WHERE 1 ORDER BY RAND() LIMIT 1");
    $row = mysqli_fetch_assoc($query);
    $foundId = (int) $row['postid'];
}

mysqli_query($mysql, "DELETE FROM cacheTable WHERE postid=".$foundId); // DELETE the record

$query = mysqli_query($mysql, "SELECT * FROM history WHERE postid=".$foundId);
$result = mysqli_fetch_assoc($query);

cacheTable will have only one column - ID (primary key) which will hold the corresponding ID (primary key) from history. cacheTable structure:

|------
|Column|Type|Null|Default
|------
|postid|varchar(100)|Yes|NULL
|-----

cacheTable will fill with all the ids from history table (it will be done once the cacheTable is empty). You will select rand result from the cacheTable and you will delete it then so it will not appear in the next selects. When we are out of records in cache table it will populate again.

NB: this approach has one major drawback - when you have new entries in history table they won't be available in cache table until it is empty and filled again.

24 Comments

What is the cacheTable Structure?
While my history table have column named postid
Now I have to play with primary keys
So postId will be the column in cacheTable.
Is it necessary to make it primary key ?
|
0

This is the code Samir Nabil Suggested :

session_start();

$_SESSION['dupes'] = array();

$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)) {
        if (!in_array($row['postid'], $_SESSION['dupes'])) {
            echo $row['postid'];
            $_SESSION['dupes'][] = $row['postid'];
        }
    }
}

3 Comments

Basically array will be renewed everytime I refresh my page
maybe I gotta set value in $_SESSION ?
If you want the array to be persistent after page refresh you could store it in a session var ... code has been updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.