如何检查数据库查询是否将返回结果? [英] How can I check if a database query will return results?

查看:131
本文介绍了如何检查数据库查询是否将返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的网站使用Perl为人力资源人员提供了一个简单的机制,以将职位空缺发布到我们的网站。它是由第三方开发的,但是他们已经接触了很久了,可惜我们内部没有任何Perl技能。当营销人员绕过其内部IT团队时,就会发生这种情况!

Our website uses Perl to provide a simple mechanism for our HR people to post vacancies to our website. It was developed by a third party, but they have been long since kicked into touch, and sadly we do not have any Perl skills in-house. This is what happens when Marketing people circumvent their in-house IT team!

我需要对此应用程序进行简单的更改。目前,空缺页面上显示我们目前有以下空缺:,无论是否有空缺!因此,我们希望对其进行更改,以便仅在适当的时间显示此行。

I need to make a simple change to this application. Currently, the vacancies page says 'We currently have the following vacancies:', regardless of whether there are any vacancies! So we want to change it so that this line is only displayed at the appropriate times.

很显然,我可以开始学习一些Perl,但是我们已经规划替换站点,它肯定不会使用Perl。因此,由于该解决方案对于那些具有这些技能的人而言是微不足道的,所以我想寻求一些集中的帮助。

I could, obviously, start to learn a bit of Perl, but we are already planning a replacement site, and it certainly won't be using Perl. So since the solution will be trivial for those with these skills, I thought I'd ask for some focused help.

下面是列出职位空缺的过程的开始。

Below is the start of the procedure that lists the vacancies.

sub list {
  require HTTP::Date;
  import HTTP::Date;

  my $date = [split /\s+/, HTTP::Date::time2iso(time())]->[0];

  my $dbh = DBI->connect($dsn, $user, $password)
    || die "cannot connect to $database: $!\n";

  my $sql = <<EOSQL;
SELECT * FROM $table where expiry >= '$date' order by expiry
EOSQL

  my $sth = $dbh->prepare($sql);
  $sth->execute();


  while (my $ref = $sth->fetchrow_hashref()) {
    my $temp  = $template;
    $temp     =~ s#__TITLE__#$ref->{'title'}#;

    my $job_spec = $ref->{'job_spec'};

...etc...

关键行是 while(我的$ ref = $ sth-> fetchrow_hashref()){。我想这是在说‘虽然我可以从返回的记录集中提取另一个空缺...。如果我将打印语句放在此行之前,它将始终显示;

The key line is while (my $ref = $sth->fetchrow_hashref()) {. I'm figuring that this is saying 'while I can pull off another vacancy from the returned recordset...'. If I place my print statement before this line, it will always be shown; after this line and it was be repeated for every vacancy.

我如何确定有一些空缺要显示,而不会过早地返回返回的记录集?

How do I determine that there are some vacancies to be displayed, without prematurely moving through the returned recordset?

我总是可以在while循环中复制代码,并将其放在if()语句(在while循环之前)中,该语句还将包括我的print语句。但是,我只想使用如果有任何记录,然后打印我们当前有..行的简单方法。不幸的是,即使是这条简单的代码,我也不知道要编写代码。

I could always copy the code within the while loop, and place it within an if() statement (preceding the while loop) which will also include my print statement. But I'd prefer to just have the simpler approach of If any records then print "We currently have.." line. Unfortunately, I haven't a clue to code even this simple line.

看,我告诉你这是一个微不足道的问题,即使考虑到我拙劣的解释!

See, I told you it was a trivial problem, even considering my fumbled explanation!

TIA

Chris

推荐答案

一个非常简单的方法是:

A really simple way would be:

$sth->execute();

my $first = 1;
while (my $ref = $sth->fetchrow_hashref()) {
    if( $first ) {
        print "We currently have the following vacancies:\n";
        $first = 0;
    }
    my $temp  = $template;
    ...
}
if( $first ) {
    print "No vacancies found\n";
}

这篇关于如何检查数据库查询是否将返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆