http://www.perlmonks.org?node_id=376196

rsiedl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Could anyone tell me if it is possible to insert a perl array (or hash) into a mysql table?

Here is the script I tried to test it with but it fails on the insert...

#!/usr/bin/perl use DBI; use strict; use warnings; my $id = 1; my @old_array = ("blah0", "blah1", "blah2"); print "Values in original array:\n"; foreach (@old_array) { if ($_) { print "\t- ", $_, "\n" } } # end-foreach my $dbh = &DB_OPEN('test','localhost','3306','user','password'); ################################# # Array Table structure # ################################# # id # array # ################################# # 1 # @array1 # # 2 # @array2 # # etc.... # ################################# my $ins_array = $dbh->prepare("insert into array_table (id,array) valu +es (?,?)"); $ins_array->execute($id,@old_array); $ins_array->finish; my $sel_array = $dbh->prepare("select array from array_table where id= +?"); $sel_array->execute($id); my (@new_array) = $sel_array->fetchrow; $sel_array->finish; print "Values in array from DB:\n"; if (@new_array) { foreach (@new_array) { if ($_) { print "\t- ", $_, "\n" } # end-if } # end-foreach } # end-if sub DB_OPEN { my ($db_name,$host_name,$port,$db_user,$db_pass,) = @_; my $database = "DBI:mysql:$db_name:$host_name:$port"; $dbh = DBI->connect($database,$db_user,$db_pass); } # end-sub exit;

Cheers,
Reagen