For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > search and replace a SQL query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author search and replace a SQL query
goldenplec@gmail.com

2006-01-10, 4:01 am

i have a sql query in a variable called $sql

i want to search through the query and for every placeholder insert a
variable

Example

$sql = select * from table where user = ? id = ? address = ? name = ?

and i want to replace the question marks with a variable to complete
the query

so ive tried

$sql =~ s/"?"/$user/;
$sql =~ s/"?"/$id/;
$sql =~ s/"?"/$address/;
$sql =~ s/"?"/$name/;

which i thought would go in to $sql the first time, find the first ?
and put the contents on $user in place of the ? then the second time
there would only be 3 question marks left so the second line of code
should replace the second question mark with the content of the next
variable.

could anybody push me in the right direction here. its starting to
drive me nuts

usenet@DavidFilmer.com

2006-01-10, 4:02 am

goldenplec@gmail.com wrote:
> i want to search through the query and for every placeholder insert a variable


I'm not sure you really want to do what I think you're saying you want
to do, but never mind that...
> <snip>
> $sql =~ s/"?"/$user/;


Question marks are special characters in Perl regular expressions (see
perldoc perlre). If you want to treat them as string literals, you need
to escape them (with backslashes), not quote them:

$sql =~ s/\?/$user/;

Cheers!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com