Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to randomize return from mysql query...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i have a database with 4 entries, and i would like to display only 2 of them, randomly.

here's my code:

Code:
$dbh = DBI->connect("dbi:mysql:mydata","myuser","mypass");

$sth = $dbh->prepare("select oID from mytable");
$sth->execute;
$cnt = $sth->rows;
$dbh->disconnect();
use constant XOID => 0;
$y=0;

 while (@row = $sth8->fetchrow_array()) {

 $xid=$row[XOID];

print << "EOF";

here's where is show the results

EOF

$y++;

}

i'm looking online and trying to figure this out.

any ideas?

- g
 
i got it.

Code:
ORDER BY RAND() LIMIT 2

curiously, will won't return duplicate records, right?

thanks!

- g
 
no, what happens is that it sorts all the rows in the table into random sequence, and then returns just 2 of them

so the only dupes that might occur would be actual dupe rows

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top