The thing I both love and dislike about programming is that in most
cases the answer is painfully obvious when presented to me!!  The
various methods I'd tried involved using rand but I never thought to
just do rand($rows)...I should have thought of that!!

Thanks Jeff, I found both suggestions to contain useful information, I
am now well on my way to my next challenge.

For what its worth, I think more examples like this in the FAQ would be
of great help.

Steve

On Sun, 2002-06-02 at 01:05, Jeff Zucker wrote:
> Jeff Zucker wrote:
>  >
> 
> > Steve Tyler wrote:
> > 
> >> - I have a (postgresql) DB table containing quoteId, quote, author
> >> - I want to have a randomly selected quote appear on a web page
> > 
> > How about:
> > 
> >   my $ids = $dbh->selectcol_arrayref("
> 
> [snip]
> 
> Or, given the fact that PG can return the number of rows without 
> explicitly fetching and that it has a limit clause, this would work with 
> fewer explicit fetches than my previous example, but wouldn't be as 
> portable:
> 
>    my $sth=$dbh->prepare("SELECT quoteID FROM quotes");
>    $sth->execute;
>    my $rows = $sth->rows;
>    $sth->finish;
>    my $random_id = int rand($rows) ;
>    print $dbh->selectrow_array("
>        SELECT quote FROM quotes LIMIT 1, $random_id
>    ");
> 
> -- 
> Jeff
> 




Reply via email to