in reply to mysql search statement
my $sql ='select from table where' . join(' and ', ( " username = $form_username", "password = $form_password", "name = $form_name", ) );
Update: listen to talexb. This really is something you should let the database handle. Alternatively, I would recommend just using your code and take out the last AND ... sure it isn't elegant, but, if and only if you have a few form variables, well, it does get the job done.
I have been toying with hash to solve this problem, but it's tricky, mainly because undef is slightly different than the empty string ... which is something you have to consider with this problem. My idea is to have the database column names as the keys, and the form variables as the values - if i can find a convienient way to remove all keys whose values are undefined or empty strings, then creating the SQL statement will be a sinch ... back in a few ...
Okay, here it is - i doubt i'll ever use, but it was fun anyways ;)
Hope this helps somehow - check out Coding Errror for more info (like bind vars).my %hash = ( username => $form_username, password => $form_password, name => $form_name, ); # was trying to do this with map ... but time is short ;) for (keys %hash) { delete $hash{$_} unless $hash{$_}; } my $sql = 'select from table where ' . join(' and ', map { qq|$_ = "$hash{$_}"| } keys %hash);
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: mysql search statement
by Parham (Friar) on Dec 31, 2001 at 02:02 UTC | |
by talexb (Chancellor) on Dec 31, 2001 at 02:12 UTC | |
|
Re: (2): mysql search statement
by dmmiller2k (Chaplain) on Jan 01, 2002 at 02:00 UTC |