Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Search breaks based on search string

by graff (Chancellor)
on Sep 01, 2005 at 20:40 UTC ( [id://488492]=note: print w/replies, xml ) Need Help??


in reply to Search breaks based on search string

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.

Replies are listed 'Best First'.
Re^2: Search breaks based on search string
by Kiko (Scribe) on Sep 01, 2005 at 21:29 UTC
    Thanks for your reply!
    The @_ is unnecessary, and clean_sql() replaces SQL and ODBC characters with ASCI code, i.e, $sql=~s/\'/\' \& chr\(39\) \& \'/g;

    Ok, so i'm using your code and i am getting the same results. when i search for status='DEPLOYED' or emp_user_name='*', it hangs and times out. I'll keep playing around with it tonight, i'll let you guys know what i find. BTW, running the SQL statement agains Access works just fine. I might have to switch to MYSQL : ). Thanks for all the replies... -Kiko

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://488492]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-16 13:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found