PDA

View Full Version : 1 random record, please help


Skeeter87
02-26-2005, 01:58 PM
Im using these lines to retrieve records from my database.. It works fine but I want to display 1 RANDOM record at a time which meets all criteria. To be honoust I dont have a clue :confused: how to display 1 random record every time..but I'm sure you can help me !?


#!/usr/bin/perl
use CGI;
$q=new CGI;
print "Content-Type: text/html\n\n";

$name = "goods";
die "Missing DBM name.\n" unless $name;
dbmopen(%map,$name,0666);
$rec = 0;
while (($key,$val)=each(%map)) {

($item1,$item2,$item3,$item4) = split("\t",$val);
if ($item2 eq "Radio") {

## check $item3 for "philips"
print qq{$item2<br>};
}}
dbmclose(%map);
exit;

Skeeter87
02-27-2005, 11:41 AM
Hmm, anyone who can help me ?

Let me rephrase my question:

Im using these lines to retrieve records from my database.. It works
fine but I want to display only 1 RANDOM record at a time, which
ofcourse meets all criteria. To be honoust I dont have a clue how to
display 1 random record at a time..but I'm sure you can help me !?

mlseim
02-27-2005, 05:04 PM
I've searched everywhere, but this is all I could find:

http://perlmonks.thepen.com/How%20do%20I%20select%20a%20random%20line%20from%20a%20file%3F.html

There is no examples on how to use it, and I really don't know myself.
.... and I've never used dbm myself either.

Puffin the Erb
02-27-2005, 06:31 PM
How about using the sql query to generate the random record?

SELECT * FROM table WHERE field = criterion ORDER BY rand() limit 1

Skeeter87
02-27-2005, 07:38 PM
ehm my host doesnt support sql and even if it did i dont know how to work with it :confused: