pc0019 has asked for the wisdom of the Perl Monks concerning the following question:
## get a list of the field names my $keys = join (', ', @keys); ## get number of placeholders required (number of field names) my $placeholder_count = @data; ## make $placeholder_count number of ?'s with a comma after each my $placeholders = '?, ' x $placeholder_count; ## remove last space and comma $placeholders = substr($placeholders, 0, - 2); ## make a string containing each piece of data separated by commas my $data = '"' . join('", "', @data) . '"'; my $dbh = DBI->connect("DBI:mysql:database=$database;host=localhost", +$username, $password, {'RaiseError' => 1}); my $sth = $dbh->prepare("INSERT INTO $table ($keys) VALUES ($placehold +ers)") or die $dbh->errstr; $sth->execute($data) or die $dbh->errstr;
I am making a script which sends data to a MySQL database. From the code above, the penultimate line would read something like this:
my $sth = $dbh->prepare( "INSERT INTO table01 (field1, field2, field3) VALUES (?, ?, ?)" ) or die $dbh->errstr;
And on the last line, $data = '"rise", "against", "ftw"'. The problem is, I get the message:
DBD::mysql::st execute failed: called with 1 bind variables when 3 are needed
Perl is seeing my $data at $sth->execute($data) as one variable, not a list. So how could I create the variables there?
I guess there are other ways to do this, but I would like to know how to create variables within Perl with specific names anyway - getting a language to write its own code is awesome :D
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating strings with specified names
by kyle (Abbot) on Aug 05, 2008 at 21:51 UTC | |
by pc0019 (Acolyte) on Aug 07, 2008 at 20:52 UTC | |
by kyle (Abbot) on Aug 07, 2008 at 21:14 UTC | |
|
Re: Creating strings with specified names
by chromatic (Archbishop) on Aug 05, 2008 at 23:34 UTC |