On Fri, May 17, 2002 at 04:41:39PM +0800, [EMAIL PROTECTED] wrote:
> FWIW: Looking back at my original script, I had declared the variables
> $name, $age, $phone *OUTSIDE*OF* the foreach loop. That's why they were
> undefined!!!
>
> my ($name, $age, $phone);
> foreach (qw(name age phone)) {
> print ${$_};
> }
Given your code above, that isn't the problem. Where they're declared has
no effect on the definedness; it does, however, cause compile-time errors
when use strict is in effect. That is not the problem you were
encountering, though. The variables are undefined for a very simple reason:
you never assigned a value to them.
> Actually, what I was *trying* to do was dereference variables and pass
> their values to DBI->quote() to build a string. e.g.,
>
> # Build the value string 'eric','34','555-1212'
> foreach ($age $name $phone) {
> $values .= $dbh->quote($_) . ',';
> }
> # Remove the extra comma at the end
> chop $values;
>
> $dbh->do(INSERT INTO $dbfile ($column_names) VALUES ($values));
Instead of using DBI's quote method use placeholders:
$dbh->do(
"INSERT INTO $dbfile ($column_names) VALUES (?, ?, ?)",
{},
$age, $name, $phone
);
It saves most of your work. See perldoc DBI.
Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com
--
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]