# Fetch ID of unsent questions from DB and pass to @questions array. If no questions are left then instead send an error to Discord and close DB connection
my@questions=@{$dbh->selectcol_arrayref("SELECT id FROM data WHERE used = 0")};
if(@questions==0){
$webhook->execute(content=>'There are no more questions left to post — please add more or you will get this message again tomorrow!');$dbh->disconnect;die}
else{
# Pick one fetched ID at random and pass to $q_id
my$q_id=$questions[rand@questions];
# Request question text and source for selected ID and pass to $question_text and $source_text respectively
my$question_text=$dbh->selectrow_array("SELECT question FROM data WHERE id = ".$q_id);
my$source_text=$dbh->selectrow_array("SELECT source FROM data WHERE id = ".$q_id);
# Mark chosen question as used and record date
$dbh->do("UPDATE data SET used = '1','when' = '".time."' WHERE id = ".$q_id);