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
, 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!bind_param(1, $classColumnName); bind_param(2, $id)
In reply to DBI binding field names as parameters by sinterklaas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |