Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of PHP's mysql_real_escape_string() for Perl's DBI?

Could some tell me if there is a function which works the same as PHP's mysql_real_escape_string() for Perl from the DBI module?

like image 352
Phil Jackson Avatar asked Feb 05 '10 13:02

Phil Jackson


People also ask

Is mysql_real_escape_string deprecated?

This extension was deprecated in PHP 5.5. 0, and it was removed in PHP 7.0.

What is the use of mysql_real_escape_string () function?

The real_escape_string() / mysqli_real_escape_string() function escapes special characters in a string for use in an SQL query, taking into account the current character set of the connection.

Why is the use of mysqli_real_escape_string () so important?

The aim of the function mysqli_real_escape_string is to try to ensure that the data that is sent to the mysql server is safe - it attempts to remove characters that are often used in sql injection.

What is Perl DBI module?

The DBI is a database access module for the Perl programming language. It provides a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used.


2 Answers

You should use placeholders and bind values.

like image 178
Sinan Ünür Avatar answered Oct 16 '22 13:10

Sinan Ünür


Don't. Escape. SQL.

Don't. Quote. SQL.

Use SQL placeholders/parameters (?). The structure of the SQL statement and the data values represented by the placeholders are sent to the database completely separately, so (barring a bug in the database engine or the DBD module) there is absolutely no way that the data values can be interpreted as SQL commands.

my $name = "Robert'); DROP TABLE Students; --";
my $sth = $dbh->prepare('SELECT id, age FROM Students WHERE name = ?');
$sth->execute($name);  # Finds Little Bobby Tables without harming the db

As a side benefit, using placeholders is also more efficient if you re-use your SQL statement (it only needs to be prepared once) and no less efficient if you don't (if you don't call prepare explicitly, it still gets called implicitly before the query is executed).

like image 27
Dave Sherohman Avatar answered Oct 16 '22 15:10

Dave Sherohman