Hi,

My query:

$query = "select * from
(
    select * from students
    union all
    select * from students
) as tmp
order by rand() limit 5";

$myquery =   mysqli_query($db_connect, $query);
    while($students = mysqli_fetch_assoc($myquery)){
        $stdid =$students['stdid']; $name = $students['name']; $dept = $students['dept'];
        echo "<br><br>".$stdid."<br>".$name."<br>".$dept;       
    }

There are 20 rows for now. Many will be added later. I get 5 results but sometimes the results are repeated. I mean I get 5 rows, but some rows are repeated. I want the 5 results to be unique. How do I go about it?

Many thanks!

$myquery = "SELECT * FROM students order by rand(UNIX_TIMESTAMP()) limit 5";

Try using the DISTINCT keyword in your query.. for example..

SELECT DISTINCT * FROM ....
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.