Can you figure out the flaw in the below code snippet(hint:take a look at the title again)?

my $sql = "select count(*) as noOfUsers from users";
my $sth = $dbh->prepare($sql);
$sth->execute();
my $ref = $sth->fetchrow_hashref();
print $$ref{"noOfUsers"};

Answer:
SQL is case insensitive. So, even though the alias noOfUsers is in camel case, when the query results are returned, it will be in lower case. Hence print $$ref{“noofpeople“} solves the problem, but a good practise would be to lower case the alias in the query itself.

Advertisements