DBI, MySQL and apostrophes

Tomasz Chmielewski

Usenet Archive
Joined
May 23, 2005
Messages
68
Reaction score
0
Let's assume I have this code which inserts some values into a MySQL database:

my $var = "Jake's cake";
$SQL = "INSERT INTO `recipes` (`name`, `stat`) VALUES ('$var', 'ok')"; $my_db->do($SQL) or die "Error!\n";

Executing this will render an error:

DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's cake', 'ok')' at line 1 at my_script.pl line 77.
Error!

Obviously, it breaks because of an apostrophe in $var variable.

What are the ways to get around this issue?

I could prepend each apostrophe with a backslash:

$var =~ s/'/\\'/;

But I'm not sure if it's the right way to do this.

Are there any "better" approaches?
 

xhoster

Usenet Archive
Joined
Nov 27, 2004
Messages
2,238
Reaction score
0
Tomasz Chmielewski said:
Let's assume I have this code which inserts some values into a MySQL database:

my $var = "Jake's cake";
$SQL = "INSERT INTO `recipes` (`name`, `stat`) VALUES ('$var', 'ok')"; $my_db->do($SQL) or die "Error!\n";

Executing this will render an error:

DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's cake', 'ok')' at line 1 at my_script.pl line 77.
Error!

Obviously, it breaks because of an apostrophe in $var variable.

What are the ways to get around this issue?

In my opinion, the only sane way to deal with this is to use placeholder/bind variables.

$SQL = "INSERT INTO recipes (name, stat) VALUES (?, 'ok')"; $my_db->do($SQL,undef,$var) or die "Error!\n";

Xho
 

Ben Morrow

Usenet Archive
Joined
Oct 26, 2003
Messages
4,961
Reaction score
0
Quoth Tomasz Chmielewski said:
Let's assume I have this code which inserts some values into a MySQL database:

my $var = "Jake's cake";
$SQL = "INSERT INTO `recipes` (`name`, `stat`) VALUES ('$var', 'ok')"; $my_db->do($SQL) or die "Error!\n";

Executing this will render an error:

DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's cake', 'ok')' at line 1 at my_script.pl line 77.
Error!

Obviously, it breaks because of an apostrophe in $var variable.

What are the ways to get around this issue?

See 'Placeholders and Bind Values' in perldoc DBI.

Ben
 

Martijn Lievaart

Usenet Archive
Joined
Sep 19, 2003
Messages
672
Reaction score
0
I could prepend each apostrophe with a backslash:

$var =~ s/'/\\'/;

But I'm not sure if it's the right way to do this.

Are there any "better" approaches?

Just drop the apostrophes, they are not needed.

Besides, use DBI variable binding. What if $var contained "');delete from users;"? Well actually that will not work with DBI, but interpolating variables in SQL queries is a bad idea and has to be done very carefully. Variable binding is the easy way to get it right.

The following should work (untested and error handling omitted):

my $var = "Jake's cake";
$SQL = "INSERT INTO recipes (name, stat) VALUES (?, 'ok')"; my $sth = $my_db->prepare($SQL); $sth->execute($var);

HTH,
M4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top