SPUG: inserting content into mysql database
Tom Heady
spug at punch.net
Tue Oct 3 11:15:29 PDT 2006
Eric Wilhelm wrote:
> # from luis medrano
> # on Tuesday 03 October 2006 10:35 am:
>
>> anybody knows how can I fix this without removing the apostrophe?
>
> placeholders? They seem to be barely mentioned in the DBD::mysql pod,
> but at least a couple of the do() examples use them.
>
Example:
my $sth1=$dbh->prepare("INSERT INTO wp_posts(post_author, post_date,
post_date_gmt, post_content,post_title, post_status, comment_status,
ping_status,post_name, post_modified, post_modified_gmt,guid)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?)")
or die; # "Couldnt prepare statement: " . dbh->errstr;
my $rv1 = $sth1->execute($post_author,$post_date,
$post_date_gmt,$post_content,$post_title,$post_status,$comment_status,
$ping_status,$post_name,$post_modified,$post_modified_gmt,$guid);
Or:
use SQL::Abstract;
my $fieldvals = {
post_author => $post_author,
post_date => $post_date,
post_date_gmt => $post_date_gmt,
post_content => $post_content,
post_title => $post_title,
post_status => $post_status,
comment_status => $comment_status,
ping_status => $ping_status,
post_name => $post_name,
post_modified => $post_modified,
post_modified_gmt => $post_modified_gmt,
guid => $guid
};
my $sql = SQL::Abstract->new;
my($stmt, @bind) = $sql->insert("wp_posts", $fieldvals);
my $sth1 = $dbh->prepare($stmt) or die; # "Couldnt prepare statement: "
. dbh->errstr;
my $rv1 = $sth1->execute(@bind);
Tom
More information about the spug-list
mailing list