I’ve seen that this unknown column in 'field list' is a frequently occurring problem and often has a simple solution. In my case the only thing I can think of is that some of my variables need quotes, but considering I’m pulling data from an array my initial reaction is that that is wrong.
I’m experimenting with creating tables in Perl, hence results.
I can create a table ($table is declared previously) fine using:
$dbh->do("create table if not exists $table ( id int(5) not null auto_increment,
time int(2) default null,
result_1 varchar(30),
result_2 varchar(30),
result_3 varchar(30),
rating int(2) default null,
primary key (id))");
But when it comes to inserting my ‘results’:
my @results = ('abc','def','ghi');
my $r_1 = $results[0];
my $r_2 = $results[1];
my $r_3 = $results[2]; # (these results print out fine)
my $time = time;
my $insert = $dbh->prepare("insert into $table values(id,$time,$r_1,$r_2,$r_3,'')");
$insert->execute;
I get the error:
DBD::mysql::st execute failed: Unknown column 'abc' in 'field list' at ...
Should I need to add extra quotations when inserting the results from an array? Or is there another problem (probably simple!) that I haven’t seen?
Your problem is with quoting, as you suspected. However, you shouldn’t manually insert quotes into the string. Instead, this is the correct way:
The advantages of this are:
executemany times using different values. You only have toprepareonce.