Var substitution in SELECT statements
This works:
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = a1a");
This doesn't:
$c = "a1a";
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = $c");
and returns the error:
Attribute 'a1a' not found
How do you do var substitution with the Pg module in Perl?
--
Randy Perry
sysTame
Mac Consulting/Sales
on 4/23/01 9:20 PM, Randall Perry at rgp@systame.com wrote:
This works:
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = a1a");This doesn't:
$c = "a1a";
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = $c");and returns the error:
Attribute 'a1a' not foundHow do you do var substitution with the Pg module in Perl?
Whoops! Needed to quote the var as so (for $c above):
$c = "\'$c\'";
Works now.
--
Randy Perry
sysTame
Mac Consulting/Sales
On Mon, Apr 23, 2001 at 09:41:51PM -0400, Randall Perry wrote:
on 4/23/01 9:20 PM, Randall Perry at rgp@systame.com wrote:
This works:
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = a1a");This doesn't:
$c = "a1a";
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = $c");and returns the error:
Attribute 'a1a' not foundHow do you do var substitution with the Pg module in Perl?
Whoops! Needed to quote the var as so (for $c above):
$c = "\'$c\'";
Works now.
also try
$sth = $dbh->prepare("select fields from tbl where f1 = ? and f2 = ?")
$sth->execute($val1,$val2);
while ($ref = $sth->fetchrow_hashref()) {
...
}
$sth->finish();
--
don't visit this page. it's bad for you. take my expert word for it.
http://www.salon.com/people/col/pagl/2001/03/21/spring/index1.html
will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!
On Mon, 23 Apr 2001, Randall Perry wrote:
This works:
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = a1a");This doesn't:
$c = "a1a";
$res = $conn->exec("select cust, contact, user_name, email from $t where
user_name = $c");and returns the error:
Attribute 'a1a' not foundHow do you do var substitution with the Pg module in Perl?
First, I would highly recommend you investigate the perl DBI abd DBD::Pg
modules -- they handle variable placeholders quite efficiently and
correctly.
But for your problem above, surely you must use single quotes around any
string you include as a literal in an SQL query.
$res = $conn->exec("select cust, contact, user_name, email from $t
where user_name = '$c'");
--
Tod McQuillin