First off, while you showed a lot of code, you may have left out some relevant things, like: "What does 'clean_sql()' do, exactly?" and "What is in @_ when this thing runs?"
I actually expect that this line is a mistake, or at least the assignment to @_ is unnecessary:
my ($sql_bb_activity_code,...,$sql_bb_region_code)=@_;
Apart from that, some judicious use of arrays and hashes would make the code a lot shorter, more readable, and probably easier to maintain. Here's one way (which doesn't go as far as it could in terms of organizing things into data structures, but it goes a long way in the right direction), to set up the sql statement:
use strict; # I assume your app already has this somewhere
#####################
# DECLARE VARIABLES #
#####################
my $count = 0; # Count the number of rows
my @grab_results = (); # Array containing results
my $statement; # sql statement
my @error = (); # Array to store any DB errors
my $connection_status; # Tells us whether we connected to a DB or not
##################
# CGI PARAMETERS #
##################
my %bb;
my @fldnames = qw(emp_user_name bb_activity_code bb_model bb_pin
bb_phone bb_imei_esn_doc bb_status bb_region_c
+ode);
my @fldops = qw(LIKE LIKE = LIKE
LIKE LIKE = =);
$bb{$_} = clean_sql(param($_)) for ( @fldnames );
##########################
# Generate SQL statement #
##########################
my $whereclause = '';
if ( $bb{emp_user_name} ne '*')
{
my @conditions = ();
for my $i ( 0 .. $#fldnames )
{
my $fname = $fldnames[$i];
if ( $bb{$fname} ne '' )
{
push @conditions, "$fname $fldops[$i] " .
(( $fldops[$i] eq '=' ) ? $bb{$fname} : "\%$bb{$fname
+}\%" );
}
}
exit if ( @conditions == 0 );
$whereclause = 'WHERE ' . join( ' AND ', @conditions );
}
$statement = 'SELECT ' . join( ',', 'bb_id', @fldnames ) .
" FROM blackberry $whereclause ORDER BY emp_user_name";
##################################################
# Connect to the database and send sql statement #
##################################################
# ...
That much, with your later stuff tacked on, passes "perl -cw", though it has not been tested in any way beyond that.
As for your later observations about how the script hangs with the sql statement that has no "WHERE" clause, you might need to make up an ad-hoc perl script that you can run from the command line to connect to the database and just run that one query and print output to the screen, to see what happens.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.