> I know I am missing a lot in my knowledge, but I'm trying to figure
> something out & seemingly am in a hole...

MySQL can do a lot of this for you, I believe...

You want a random record from the database and retrieve a particular field
from that record:

# Untested, but you would call this subroutine like so:
# getRandomRecord( $dbh, "SELECT field_Im_interested_in FROM $TABLE
# WHERE month like $month and day like $day" );
# NOTE: $sql (2nd parameter) can't already have a LIMIT in it, or any SQL
# statement which a LIMIT suffix would result in an invalid SQL statement.
sub getRandomRecord {
    my( $dbh, $sql ) = @_;
    my ( $sth, $randomNumber, $myValue );

    $sth = $dbh->prepare( $sql );
    $sth->execute;

    $randomNumber = int(rand( $sth->rows ) );
    $sth->finish;
    # $randomNumber will be 0 <= $randomNumber < $sth->rows, which
    # fits nicely with MySQL's LIMIT clause

    ( $myValue ) = $dbh->selectrow_array( $sql . " LIMIT $randomNumber,
1" );
    return $myValue;
}

HTH & Have Fun!

Jason

> the task is as follows:
>
> 1) query a mysql database for as many records as match a criteria
> (I can do this OK)
>
> 2) put the resulting records, how ever many there are, into a list or an
> array
>
> 3) count the number of records I have retrived
> (I can do this OK)
>
> 4) choose a random record number
> (I can do this)
>
> 5) get the record corresponding to the random record number from the array
>
> 6) get a particular field from the stored record
>
> I am using DBI to get the data from the database. here is what I have so
> far:
>
> sub getRandomRecord{
>   my $cgi = shift;
>   my $dbh = shift;
>   my $month = shift;
>   my $day = shift;
>
>   my $searchResult;
>   my $returnValue;
>
>    #prepare and execute SQL statement
>     $sqlstatement = "SELECT * FROM $TABLE WHERE month like $month and day
> like $day";
>     $sth = executeSQLStatement($sqlstatement, $dbh);
>
>    $counter = 0;
>
>    # put the records returned in an array/list & count how many
>     while ($searchResult = $sth->fetchrow_array() )
>     {
>     # get the 4th field from the record in the array & put it in the list
>     my @list = ($searchResult[3]);
>      ++$counter;
>    }
>
>     # pass the counter to the random integer routine & get a value back
>     my $randomNumber = getRandomNumber($counter);
>
>     $returnValue = @list[$randomNumber];
>
>    # clean up the DBI
>    $sth->finish();
>
>    return $returnValue
>    }
>
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to