Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Prepared statements- variable number of placeholders- SOLVED
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tim Bowden  
View profile  
 More options Oct 22 2012, 4:30 am
Newsgroups: perl.dbi.users
From: tim.bow...@mapforge.com.au (Tim Bowden)
Date: Mon, 22 Oct 2012 16:11:53 +0800
Local: Mon, Oct 22 2012 4:11 am
Subject: Re: Prepared statements- variable number of placeholders- SOLVED
Thanks Henri.  Much appreciated.
On Mon, 2012-10-22 at 09:21 +0200, Henri Asseily wrote:
> The $sth is a mutable string… Which means you can change it at runtime!

> Here's an example of a modified $sth at runtime:

>   my $up_sql = defined($updated_at) ? 'updated_at < ? AND ' : undef;
>   my $sql = "SELECT * FROM mytable WHERE $up_sql type_id = ? AND serial_number IN (
>               SELECT serial_number FROM anothertable WHERE device_id = ?)";
>   my $sth = $dbh->prepare($sql);
>   my $i = 1;
>   if (defined($up_sql)) {
>     $sth->bind_param($i, $updated_at, SQL_INTEGER); $i++;
>   }
>   $sth->bind_param($i, $pass_type_id, SQL_VARCHAR); $i++;
>   $sth->bind_param($i, $device_id, SQL_VARCHAR); $i++;
>   $sth->execute;

> On Oct 22, 2012, at 9:16 AM, Tim Bowden <tim.bow...@mapforge.com.au> wrote:

> > Is it possible, and if so, how can I set the number of placeholders in a
> > prepared statement at runtime?

> > IE, given something like:

> > my $sth = $dbh->prepare("INSERT INTO $table ($fields) VALUES (?,?,?));

> > which is fine if I know how many fields I'm inserting into, but what if
> > I don't know till runtime how many fields?  How can I put the (?,?,?)
> > into a variable and have it recognised as placeholders?

> > Thanks,
> > Tim Bowden


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.