Hi Francesco

I would guess that the problem is caused by you escaping the $ signs in your
SELECT statement (eg \$fields). Try using something like:-
    $sth = $dbh -> prepare(qq{select *  from testami where '$field1'  = 
        '$value1' and '$field2' = '$value2'});

or at least remove the \ before the $variables. If you use \$field1 you are
passing the literal string '$field1' to your search, rather than the
interpolated value of $field1.

You might want to start looking into placeholders too. In this case you
would use:
    $sth = $dbh -> prepare( qq{select *  from testami where ? = ? and ? =
?});
    $sth -> execute($field1, $value1, $field2, $value2);

The ?'s are replaced (in order) by the values in the brackets after the
execute; again these will be interpolated.

Cheers

Mark C

P.S. Don't apologise for posting the script. It makes things much easier to
fix if we can see the problem. 8-)

> -----Original Message-----
> From: Francesco Scaglioni [mailto:[EMAIL PROTECTED]]
> Sent: 26 June 2001 10:10
> To: [EMAIL PROTECTED]
> Cc: [EMAIL PROTECTED]
> Subject: Re: ? embed scalars in the sql
> 
> 
> Apologies for posting a small script.  I created a little test table
> with columns testname, testsex, testage and testcolour.  The script
> runs without errors yet the value of returned rows is always zero.
> Please would someone be so kind as to point out the error of my ways.
> 
> TIA
> 
> Francesco
> 
> #!/usr/bin/perl -w
> #
> # test script to query an mysql database
> #
> use strict;
> use DBI;
> 
> my ( $dbh, $value1, $value2, $sth, @rows, $counter, 
> $field_name, $field2, $field1);
> 
> print "Enter the first fieldname (field1)  : "; chomp ($field1 = <>);
> print "Enter the second fieldname (field2)  : "; chomp ($field2 = <>);
> print "enter the value for field one   : "; chomp ($value1 = <>);
> print "enter the value for field two   : "; chomp ($value2 = <>);
> 
> $dbh = DBI -> connect ("DBI:mysql:ami","fgs") || die $DBI::errstr;
> $sth = $dbh -> prepare( qq{
>            select *  from testami where \"\$field1\"  = 
> \"$value1\" and \"\$field2\" = \"\$value2\"
> });
> 
> $sth -> execute();
>  $counter = "0";
> while ( @rows = $sth  -> fetchrow_array ())  {
>     $counter++;
>     print "@rows\n";
> }
>     print "Number of records  =  $counter\n\n";
> $dbh -> disconnect;
> 

Reply via email to