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

I have now spent 2 hours looking over this code and testing different things and can not figure out what is wrong. Testing is showing that $OldRatting[0] is getting filled with the column name from the SQL command here:
$sth = $dbh -> prepare (qq~select ? from FanRatings where Title = +?~) or die $DBI::errstr; $sth -> execute ($rating, $filename) or die $DBI::errstr; @OldRating = $sth -> fetchrow_array;
This is the full code I use. Can anyone tell me what I am doing wrong?
#!/user/perl use CGI q~:standard~; use CGI::Cookie; use CGI::Carp qw(fatalsToBrowser); use strict; use DBI; my ($dbh, $sth, $filename, $rating, @current, @VotedOn, %cookies, $Can +Vote, $value, $c, @OldRating, $result); $filename = param('name'); $rating = param('rating'); %cookies = fetch CGI::Cookie; $CanVote = 1; if (exists($cookies{'FFInfoVotedOn'})) { @VotedOn = $cookies{'FFInfoVotedOn'} -> value; foreach $value (@VotedOn) { if ($value eq $filename) { $CanVote = 0; } } } $dbh = DBI -> connect ('dbi:ODBC:', '', '') or die $DBI::errstr; if ($CanVote) { $result = 1; $sth = $dbh -> prepare (qq~select One, Two, Three, Four, Five from + FanRatings where Title = ?~) or die $DBI::errstr; $sth -> execute ($filename) or die $DBI::errstr; @current = $sth -> fetchrow_array; if (!defined $current[0]) { $sth = $dbh -> prepare (qq~insert FanRatings (Title, One, Two, + Three, Four, Five) values (?, 0, 0, 0, 0, 0)~) or die $DBI::errstr; $sth -> execute ($filename) or die $DBI::errstr; } $sth = $dbh -> prepare (qq~select ? from FanRatings where Title = +?~) or die $DBI::errstr; $sth -> execute ($rating, $filename) or die $DBI::errstr; @OldRating = $sth -> fetchrow_array; $OldRating[0]++; $sth = $dbh -> prepare (qq~update FanRatings set ? = ? where Title + = ?~) or die $DBI::errstr; $sth -> execute ($rating, $OldRating[0], $filename) or die $DBI::e +rrstr; @VotedOn = (@VotedOn, $filename); $c = new CGI::Cookie(-name => 'FFInfoVotedOn', -value => "@VotedOn", -expires => '+10Y', ); } $dbh -> disconnect;

Replies are listed 'Best First'.
Re: Variable is filled with column name not data, can't figure out why.
by grep (Monsignor) on Jul 17, 2007 at 03:20 UTC
    Straight from the DBI Docs:
    With most drivers, placeholders can't be used for any element of a statement that would prevent the database server from validating the statement and creating a query execution plan for it. For example: "SELECT name, age FROM ?" # wrong (will probably fail) "SELECT name, ? FROM people" # wrong (but may not 'fail')

    So you'll need to change your code to:

    $sth = $dbh->prepare(qq~select $rating from FanRatings where Title = ? +~) or die $DBI::errstr; $sth->execute($filename) or die $DBI::errstr;
      Now that you point that out I remember reading it last week when learning about the place holders. It just never poped back to me as the problem. Thanks for the help!