in reply to Re: mysql search statement
in thread mysql search statement

i thought i should post an answer even though khkramer had an awesome solution. I got this and thought i should share it :).
#!/usr/bin/perl -w my $username = 'joejoe'; my $password = 'lightning'; my $name = 'Joe'; my $SQL = "SELECT FROM table WHERE" . (($username)?" username = '$username'":'') . (($password)?" and password = '$password'":'') . (($name)?" and name = '$name'":'') . ';'; print $SQL;
Happy New Years all :)

Replies are listed 'Best First'.
Re(3): mysql search statement
by dmmiller2k (Chaplain) on Jan 01, 2002 at 02:11 UTC

    So, if $username is not populated (or otherwise false), but password is, you'll get:

    $SQL = "SELECT FROM table WHERE AND password='pwd'

    Just join your WHERE clauses together using the string, ' AND ' as the separator.

    my @WHERE = (); push @WHERE, "username = '$username'" if $username; push @WHERE, "password = '$password'" if $password; push @WHERE, "name = '$name'" if $name; $SQL = "SELECT FROM table WHERE " . join ' AND ', @WHERE;

    Or see my post at 'Re: (2): mysql search statement'.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime