Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,959 questions

51,901 answers

573 users

How to SELECT random records from a table in MySQL and PHP

1 Answer

0 votes
$con = mysql_connect("localhost", "user", "password");
if (!$con)
    die (mysql_error());
if ( ! mysql_select_db("database name", $con))
    die (mysql_error());

// select 10 random records (can be 5 or 100)
$sql = mysql_query('SELECT * FROM table ORDER BY RAND() LIMIT 10', $con);
if ( ! $sql ) die(mysql_error());

while($row = mysql_fetch_assoc($sql))
{
  echo $row['first_name'] . "<br>";
}


answered Oct 10, 2014 by avibootz

Related questions

1 answer 264 views
1 answer 312 views
2 answers 283 views
2 answers 660 views
3 answers 709 views
2 answers 376 views
...