简体   繁体   中英

Simple perl mysql query not working

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?

你可能意味着

$count = $return[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM