in reply to mySQL-driven Random Quote generator
This is the part of the code that I was interested in. I do believe that you can make this much simplier (and faster) with one SQL call:$rth=$dbh->prepare("select max(id) from zen order by id") or die("Can't connect: ", $dbh->errstr); $rth->execute; $id=$rth->fetchrow_array; $selid=int(rand($id)+1); $tth=$dbh->prepare("select koan from zen where id=$selid") or die("Can't connect: ",$dbh->errstr); $tth->execute; $koan = $tth->fetchrow_array;
("order by rand()" is mentioned in chapter 7 of the mysql doc, and I used this trick for a random quote generator of my own.)$tth=$dbh->prepare("select koan from zen order by rand() limit 1") + or die("Can't connect: ",$dbh->errstr); $tth->execute; $koan = $tth->fetchrow_array;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: mySQL-driven Random Quote generator
by Hero Zzyzzx (Curate) on Mar 31, 2001 at 02:20 UTC |