mattdope has asked for the wisdom of the Perl Monks concerning the following question:

Hi have a slight problem and would appreciate any suggestions. I need to use perl to search a database but require to search using unknown strings. For example a name that has an 'ME' character sequence. I have currently retrieved all of the names in the database into an array
my @row = $sth->fetchrow_array;
and i'm now using perl to search the array but there is no way of actually printing specific names. A'm I going about this all wrong? I'm it isn't possible to search like this within the mysql query. If anyone could point me in the right direction I would be most grateful.

Replies are listed 'Best First'.
Re: DBD::Mysql select
by davorg (Chancellor) on Sep 04, 2006 at 14:37 UTC

    This is the kind of problem that is much better solved in SQL than in Perl.

    SELECT some_columns FROM some_table WHERE name like '%ME%'
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: DBD::Mysql select
by McDarren (Abbot) on Sep 04, 2006 at 14:42 UTC
    "I'm it isn't possible to search like this within the mysql query"

    Search like what?
    It seems to me that you could do something like:

    SELECT foo FROM bar WHERE name LIKE '%ME%';

    But that's just a guess.
    Show us a bit more of the code you have, including the current query that you are using.

    Of course, to pull elements from an array that match a certain criteria is quite simple. Something like:

    for (@array) { print "$_ matches\n" if $_ =~ /ME/; }

    But you'll probably find that you don't need to do that, if you construct your SQL carefully.

    Cheers,
    Darren :)

      Thanks for those suggestions. I need to search for names that are a certain length (eg 5,6 characters) aswell. Can this be done using mysql?

        Don't mean to sound rude, but did you consider looking in the documentation?

        SELECT some_columns FROM some_table WHERE CHAR_LENGTH(some_column) = 5
        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg