CGI/DBI no results marker

Posted on 2005-03-01
Medium Priority
Last Modified: 2013-12-25
I'm running a CGI script that uses Perl DBI to check for records in an access database.  I'd like the users to get a message if their search doesn't have any matches, but I can't find a way to get it to work.  The criteria are entered via html form, then used to build a SQL statement that searches through the ODBC to the access DB.  Pretty simple stuff -- the problem is that I have a

while (@row=$sth->fetchrow_array)

statement in there for getting results.  I tried running comparisons on the contents of @row, but since no results in the DB causes a failure in the while loop, that's no good.  I've tried putting the assignment into an if statement, but then if it succeeds, I lose the first match from the search.

All I want is a way to check whether the SQL statement got any results from the database, and display a quick note if not.  I'm thinking there's got to be a simple way to do it, but I haven't stumbled my way into it yet.
Question by:Zeek0
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Accepted Solution

kandura earned 2000 total points
ID: 13435123
You could use a flag for this:

$no_hits = 1;

while(@row = $sth->fetchrow_array) {
   $no_hits = 0;

if($no_hits) {
  print "Search returned no results";

Or something like that.
LVL 28

Expert Comment

ID: 13435864
I have not tested this but I think it should do what you need.

$rows = $sth->fetchall_arrayref();

if ($rows) {
    foreach my $row (@$rows) {
        #  process each row
else {
    print "No search results";

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question