use Memoize;
use DateTime::Format::MySQL;
use autouse 'Data::Dumper' => qw(Dumper);
+use DBI qw(:sql_types);
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
$sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
$sth->execute;
my $range = ($sth->fetchrow_array)[0];
- if ($range > 1) {
- # chose a random id within that range if there is more than one quote
- my $id = int(rand($range));
- # grab it
- $query = 'SELECT * FROM quotes WHERE id = ?;';
- $sth = C4::Context->dbh->prepare($query);
- $sth->execute($id);
- }
- else {
- $query = 'SELECT * FROM quotes;';
- $sth = C4::Context->dbh->prepare($query);
- $sth->execute();
- }
+ # chose a random id within that range if there is more than one quote
+ my $offset = int(rand($range));
+ # grab it
+ $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
+ $sth = C4::Context->dbh->prepare($query);
+ # see http://www.perlmonks.org/?node_id=837422 for why
+ # we're being verbose and using bind_param
+ $sth->bind_param(1, $offset, SQL_INTEGER);
+ $sth->execute();
$quote = $sth->fetchrow_hashref();
# update the timestamp for that quote
$query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';