I am having problem with a select statement that works fine if I run it in MySQL but not from within the perl script I have written.

The script collects data from a file into a hash and when it comes across a blank line passes the hash to a subroutine that updates the database.

I have checked that all the data in the hash is being passed properly to the subroutine and all looks good but the SELECT statement doesn't return any rows. I am fairly new to perl so any help would be gratefully received.

If you are expecting $sth->rows to return the data, you will wait a long time . The rows method just returns the number of rows affected by the statement. I'm not sure if it actually returns anything useful for selects. To get the data, you need something like:

Looking at the DBI perldoc, if you want $sth->rows to return the number of rows returned by your query, you have to get them all at the same time with $sth->fetchall_hashref() or $sth->fetchall_arrayref().

UPDATE 2:

Of course, once you do that, you could just count the entries in the result structure directly.