Re: Problem with DBI and placeholders

Andrew Solomon <[email protected]>
Newsgroups gmane.comp.lang.perl.beginners
Message-ID <CAN=twKv6j1VVpfbD12LQQ3hD7BY_RRkJs+D=Mzet+P1QKNwa6A@mail.gmail.com>
The ? is for quoting field values rather than entering table or field
names. "SELECT * FROM $table" is probably what you want.

On Fri, Apr 9, 2021 at 12:49 PM mailing lists via beginners <
[email protected]> wrote:

> Hello,
>
> I am having a problem with a very simple task and I don't see where the
> fault is.
>
> The first query works with problem but the second one fails with this
> error:
>
>  ./test.pl
> OK 1
> DBD::mysql::st execute failed: You have an error in your SQL syntax; check
> the manual that corresponds to your MySQL server version for the right
> syntax to use near ''test'' at line 1 at ./test.pl line 28.
> END
>
>
> Does anyone know where the problem is?
>
>
>      1    #!/usr/bin/perl
>      2    use strict;
>      3    use warnings;
>      4    use 5.010;
>      5    use DBI;
>      6
>      7    my %db = (
>      8       host     => 'mysql.local',
>      9       user     => 'app',
>     10       password => '12345678',
>     11       db       => 'test_db',
>     12    );
>     13
>     14    my $dbh = DBI->connect(
>     15
> 'dbi:mysql:database='.$db{'db'}.';host='.$db{'host'}.';port=3306',
>     16                                $db{'user'},
>     17                                $db{'password'},
>     18                                { AutoCommit => 1, },
>     19                              )
>     20                  or die "Can't connect to database: $DBI::errstr\n";
>     21
>     22
>     23    my $sth = $dbh->prepare("SELECT * FROM test");
>     24    $sth->execute() && say "OK 1";
>     25
>     26    my $table="test";
>     27    $sth = $dbh->prepare("SELECT * FROM ?");
>     28    $sth->execute($table) && say "OK 2"
>     29
>     30    say "END";
>
>
>
>
>

-- 
Andrew Solomon
Director, Geekuni <https://geekuni.com/>
P: +44 7931 946 062
E: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.