Home: Perl Programming Help: DBI:
Random select



skirrow
stranger

Apr 7, 2002, 3:57 PM


Views: 6526
Random select

Hi,

I want to randomly select a record from a table and display it, how do I do this?

The fields in the table are, Comment and Name. At present, I'm using this code but obviously it will display all the records, not just one randomly selected record.


Code
 my $sth = $dbh->prepare(<<End_SQL) or die "Couldn't prepare statement: $DBI::errstr; stopped"; 
SELECT Comment, Name FROM `testimonials`
End_SQL
$sth->execute() or die "Couldn't execute statement: $DBI::errstr; stopped";
while ( my ($comment, $name) = $sth->fetchrow_array() ) {
print "<tr><td><font face="Verdana,Arial,Helvetica" size="1"><i>\"$comment\"</i></font></td></tr>\n";
print "<tr><td align=right><font face="Verdana,Arial,Helvetica" size="1"><b>$name</b></font></td></tr>\n";
}



All assistance is appreciated.

- Neil
--
http://www.locked-area.com/
http://www.opencrypt.com/


uatt
User

Apr 8, 2002, 4:50 AM


Views: 6520
Re: [skirrow] Random select

you may use this


Code
  

select Comment, Name from testimonials order by rand() limit 0,1