Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Search breaks based on search string by graff
in thread Search breaks based on search string by Kiko

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found