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

Dear Monks,
I have an array like:
my @array_num = ("123, 456, 345, 459, 234"); my $sql = my $sql= "SELECT tb.num1, tb.num2 FROM MYTABLE WHERE tb.num +1=$n1 AND tb.num2=$n2 ";

I need to use the value for the variable "$n1" with the values from @array_num.
How could I use that without crash or have a slow OBDC connection?
Thank you!!

Replies are listed 'Best First'.
Re: Array, SQL Question!
by Yendor (Pilgrim) on Nov 11, 2004 at 20:28 UTC

    First, your @array_num variable is getting populated with a single value the way that your code is written. Try this:

    my @array_num = qw/123 456 345 459 234/;

    Second, you never set $n1 and $n2 to anything. I'll assume that you want them to be the first and second values in your array.

    my $n1 = $array_num[0]; my $n2 = $array_num[1];

    Third, you really should be using placeholders (or bind parameters) in your $sql statement to safeguard against SQL injection attacks. This is not much of a problem in the statements as you've presented them, but it's really a good practice to get into. Read more about SQL placeholders.

    So, finally, we have this:

    my @array_num = qw/123 456 345 459 234/; my $n1 = $array_num[0]; my $n2 = $array_num[1]; my $sql= " SELECT tb.num1, tb.num2 FROM MYTABLE tb WHERE tb.num1 = ? AND tb.num2 = ?"; $sql->execute($n1, $n2);

    EDIT: Per trammell's reply below.

      I think you want e.g.:
      my $n1 = $array_num[0]; my $n2 = $array_num[1];
      I dont want the value of $n2 to be part of the array just values from $n1

        Your original node didn't state what $n1 or $n2 were supposed to be. Multiple respondents (myself included) assumed that you made a mistake in the assignment of your @array_num variable. Furthermore, I stated that I was making an assumption about what you wanted in $n1 and $n2.

        I've re-read your original post as well as your reply to mine, and I still don't know what you want for $n1 and $n2. I'm going to now assume that you know what they are, as well as how to populate them, unless you give more specific information about what you want in them.

Re: Array, SQL Question!
by hardburn (Abbot) on Nov 11, 2004 at 20:18 UTC

    Small quibble, though one that might save you hours tracking down a silly problem. Did you really mean to have a one-element array? I'm guessing you didn't mean to have quotes there.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Array, SQL Question!
by pg (Canon) on Nov 11, 2004 at 20:13 UTC

    Prepare your statement with placehold first, when you execute, go through your array, and pass them in.

    my $sql= "SELECT tb.num1, tb.num2 FROM MYTABLE tb WHERE tb.num1=? AND + tb.num2=? "; $dbi->prepare($sql);

    our query has a little problem, and you have to explicitly declare tb as an alias of MYTABLE as I did above.

    You could also use in operator, depends on your need.

Re: Array, SQL Question!
by fglock (Vicar) on Nov 11, 2004 at 20:21 UTC

    Please note that your array has a single element, which is a string containing
    "123, 456, 345, 459, 234" - if you want a list of numbers, use:

    my @array_num = ( 123, 456, 345, 459, 234 );
Re: Array, SQL Question!
by eric256 (Parson) on Nov 12, 2004 at 00:28 UTC

    Like others have said it would be easier with a better idea of what it is you would like to do. Maybe just explain what you would like to accomplish. That said, i guessed that you meant you want to find " all elements in MYTABLE where num1 is in the set @array_num and num2 = some value." The solution to that problem is as below. You want to use the the ? placeholder because its a realy good idea. So i generate an IN statment using the lenght of @array_num to create the correct number of ?. Then prepare and execute the query.

    my @array_num = (123, 456, 345, 459, 234); my $sql = 'SELECT tb.num1, tb.num2 FROM MYTABLE tb WHERE tb.num1 IN (' + . join(',', ('?') x @array_num) . ') AND tb.num2=?'; my $select = $dbi->prepare($sql); $select->execute(@array_num, $n2);

    This generates the sql statment SELECT tb.num1, tb.num2 FROM MYTABLE tb WHERE tb.num1 IN (?,?,?,?,?) AND tb.num2=? and fills in the ? with the correct values.


    ___________
    Eric Hodges