简体   繁体   English

简单的perl mysql查询不起作用

[英]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. 我已经离开了mysql和perl游戏已有好几年了,似乎无法做到这一点。 I have a table with just 3 columns. 我有一个只有3列的表。 'cnt' is one of them. 'cnt'就是其中之一。 All I want to do is query the table on 'name' and see if name exists. 我想做的就是在'name'上查询表,看看是否存在名称。 If it does, I want to capture the value of 'cnt'. 如果是,我想捕获'cnt'的值。 The table has a record of testName with a value of 2 I added manually. 该表有一个testName记录,值为2我手动添加。 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 根据mysql上的perl doc

An alternative to fetchrow_arrayref. 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. 因为你选择cnt作为返回值,所以@return的大小是1,但你误解了它是符合你的查询条件的结果数。不,不是这样!请仔细阅读perl doc 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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