Hi. I hope the title of this post isn't misleading to any of you. I'm trying to collect some data for (a) variable (number of) columns, so I figured I'd first prepare the query (only once) and then pass on the relevant column inside the loop. However, this returns the actual column name, whereas I obviously want the value for this column. I can get it to work and do what I want if I prepare the query inside the loop, but this is exactly what I was trying to avoid. Could anyone explain me why the following doesn't return the value, but the column name?

my $sClassScoresQuery = qq/SELECT ? FROM $exportDb->{pagesTable} WHE +RE id=?/; my $stPageClassScores = $exportDb->{dbh}->prepare($sClassScoresQuery +); my $newClassScore = 0; for my $className (keys $hrClassName2Id){ my $classColumnName = $className . "_score"; my $classId = $hrClassName2Id->{$className}; my @aTemp; for my $id (@pageTableAutoIncrementIds){ #prepare here... for debugging purposes only! #my $sQuery = qq/SELECT $classColumnName FROM $exportDb->{pagesT +able} WHERE id = $id/; #my $stDebug = $exportDb->{dbh}->prepare($sQuery); $stPageClassScores->execute($exportDb->{dbh}->quote_identifier($ +classColumnName), $id); #$stDebug->execute(); while (my @res = $stPageClassScores->fetchrow_array()){ #while (my @res = $stDebug->fetchrow_array()){ push @aTemp, $res[0]; } } #some other stuff happens here... }

So the above code returns the columnname in @res (instead of the value for this column). It works alright if I use the lines currently commented out instead. But this means preparing the statement all the time. I have tried it with a

bind_param(1, $classColumnName); bind_param(2, $id)
, and even bind_param(1, $dbh->quote_identifier($classColumnName)) just before running execute, but that didn't help either. Like I said; could someone maybe explain me what's going on here and why this approach doesn't work? Thanks!


In reply to DBI binding field names as parameters by sinterklaas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.