1

I've been out of the mysql and perl game for quite a few years and can't seem to get this right. I have a table with just 3 columns. 'cnt' is one of them. All I want to do is query the table on 'name' and see if name exists. If it does, I want to capture the value of 'cnt'. The table has a record of testName with a value of 2 I added manually. When this script is run it returns empty.

my $count;
my $pop = qq(SELECT cnt FROM popular WHERE name="testName"); 
my $sth = $dbh->prepare($pop); 
$sth->execute() or die $dbh->errstr; 

my @return; 

while (@return = $sth->fetchrow_array()) { 
   $count = $return[1]; 
} 

print "our return count is $count";

Is it obvious to anyone what I did wrong?

3
  • does "returns empty" mean it doesn't even print out "our return count is"? Commented Nov 22, 2013 at 3:16
  • It prints out the string but it stops after "is". $count doesn't contain anything. Commented Nov 22, 2013 at 3:20
  • 2
    Crossposted at PerlMonks. Commented Nov 22, 2013 at 3:57

2 Answers 2

3

You probably mean

$count = $return[0];
Sign up to request clarification or add additional context in comments.

Comments

0

According to perl doc on mysql

An alternative to fetchrow_arrayref. Fetches the next row of data and returns it as a list containing the field values. Since you select cnt as the return value ,so , the size of @return is 1,but you misunderstand it as the number of results which meets your query condition.No, it is not so!Please have a more careful reading of perl doc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.