I'm trying to insert into MySql, using DBI/Perl... What I'm doing is storing all my Print statements into an array, then dumping them out to print, and inserting into a MySql table in a foreach loop... Print resolves ok, but the MySql inserts are being split into fields at the breaks... Example :

here's a couple "push" statements, that I'm pushing into my print array...

push (@printarray, '<tr><td><input name=',"vadate$x",' size="20" maxlength="20"></td>');
push (@printarray, '<td><textarea name=',"vacom$x",' COLS=40 ROWS=3></TEXTAREA>');

these print ok, but they insert into MySql as 3 different records apiece...

How do I tell MySql NOT to look at commas, quotes, etc., and just insert the string "as is"? I presume there's such a way, but I'm not able to locate it...

Not looking for code help, but an RTFM direction would be nice...

thanks...

Roy

After creating and populating the @printarray, connecting to MySQL, selecting a database etc. the following snippet seems to work OK. The trick is to prepare your query with placeholders and letting DBI worry about what characters need escaping.

$sth=$dbh->prepare
             ("INSERT INTO test (id, html_string)
               VALUES           (? , ?)");

my ($id, $html);

$id = 1; #Or any unique integer
$html = join '', @printarray; #concatenate array elements into string with no separator
$sth->execute($id, $html) 
    || die "Couldn't insert record : $DBI::errstr";

Placeholders

actually, I use placeholders already... the problem is the "Push" is creating 3 elements per line... I didn't realize this until I printed the count of the array... I was able to remedy that, but I had to QW the line being "pushed" into the array... then, when I wanted to unload it, I couldn't get it to interpolate... That may be a problem... I was just thinking to myself, what the heck was I thinking that this would be easier? So, if I have a db table with lines like you'd normally use to print in Perl, any ideas on how I could get them to interpolate (the embedded Perl variables)???

Do you want to interpolate the variables before inserting the data into the table, or after reading it from the table? Or does it matter? Can we assume the value of $x is the same when inserting the record as when retrieving it?

I don't know how to accomplish the latter (reading a database record and recognising and evaluating parts of a string field as embedded Perl variables.) I would guess it involves searching for another module on CPAN to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.