my $sth = $dbh->prepare( q{SELECT * FROM $table})
|| die "Can't prepare statement: $DBI::errstr";
...you have the query single quoted which does not interpolate your $table variable, try this:
my $sth = $dbh->prepare( qq{SELECT * FROM $table})
|| die "Can't prepare statement: $DBI::errstr";
...notice the "qq{" which is double quotes and will interpolate $table.
-- hiseldl What time is it? It's Camel Time! | [reply] [d/l] |
my $quoted_table = $dbh->quote_identifier($table);
my $sth = $dbh->prepare(qq{SELECT * FROM $quoted_table})
or die "Can't prepare statement: $DBI::errstr";
| [reply] [d/l] |
Thank you so much for the input, it works fine.
| [reply] |
How do we pass the table name as a variable in run time.
If you want to specify a table name dynamically, you have to construct it into the query before you prepare the query. Interpolation works just fine for this, though you'd be advised to untaint the table name beforehand, lest someone sneak in something really nasty.
If you look around a bit, you'll ocassionally see people doing something like
$sth = $dbh->prepare("select * from ?");
$sth->execute($name);
On the surface, this appears to work (for some databases), but the effect is a coincidence of implementation. DBI/DBD does not make sure that bind parameters are in syntactically valid places in the query. That test is deffered to the database. Some databases don't support query preparation. For these, the DBD arranges to hang on to whatever query you pass to prepare(). When you execute(), the save query is massaged to expand quoted bind parameters, and is passed to the database for execution. Non valid parameter substitution appears to work, until you target a database that does support query preparation. Then, KaBOOM.
| [reply] [d/l] [select] |
| [reply] |