CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
Random select

 



skirrow
stranger

Apr 7, 2002, 3:57 PM

Post #1 of 2 (3625 views)
Random select Can't Post

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

Post #2 of 2 (3619 views)
Re: [skirrow] Random select [In reply to] Can't Post

you may use this


Code
  

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


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives