I strongly suspect one cannot use placeholders for table names due to quoting. The resulting SQL with placeholders will be syntactically incorrect:
select col1 from 'foo' where col1 = '3'
A complete sample (trace output filtered for readability):
mysql> use test; mysql> create table foo (col1 integer); mysql> insert into foo values (1),(2),(3); #!/usr/bin/perl -wls use strict; use warnings; our $user ||= undef; our $pass ||= undef; use DBI; my $dbh = DBI->connect(q{DBI:mysql:test}, $user, $pass) or die "connect failed"; my $sth = $dbh->prepare("select col1 from ? where col1 = ?"); $sth->trace( 2 ); my $table = 'foo'; my $value = 3; $sth->execute($table, $value) or die("execute failed"); __END__ $ ./646467.pl -user ***** -pass ***** mysql_st_internal_execute MYSQL_VERSION_ID 40120 Binding parameters: select col1 from 'foo' where col1 = '3' --> do_error You have an error in your SQL syntax; check the manual that correspond +s to your MySQL server version for the right syntax to use near ''foo +' where col1 = '3'' at line 1 error 1064 recorded: You have an error +in your SQL syntax; check the manual that corresponds to your MySQL s +erver version for the right syntax to use near ''foo' where col1 = '3 +'' at line 1 <-- do_error
However, textual substitution before the prepare works as expected:
#!/usr/bin/perl -wls use strict; use warnings; our $user ||= undef; our $pass ||= undef; use DBI; my $dbh = DBI->connect(q{DBI:mysql:test}, $user, $pass) or die "connect failed"; my $table = 'foo'; my $sth = $dbh->prepare("select col1 from $table where col1 = ?"); my $value = 3; $sth->execute($value) or die("execute failed"); while (my $href = $sth->fetchrow_hashref()) { foreach my $key (keys %{$href}) { print qq{$key: } . $href->{$key}; } } $sth->finish(); $dbh->disconnect(); __END__ $ ./646467.pl -user ***** -pass ***** col1: 3
Environment:
mysql Ver 14.7 Distrib 4.1.20, for redhat-linux-gnu (i686) using read +line 4.3 Linux 2.6.9 i686 Module id = DBI INST_VERSION 1.56
--
Andreas

In reply to Re^3: DBI query where table name is a variable by andreas1234567
in thread DBI query where table name is a variable by ai56

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.