Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mysql_real_escape_string function in PHP

So in this program I'm writing, I actually grab a SQL query from the user using a form. I then go on to run that query on my database.

I know not to "trust" user input, so I want to do sanitization on the input. I'm trying to use mysql_real_escape_string but have been unsuccessful in getting it to work.

Here's what I'm trying, given the input: select * from Actor;

//"query" is the input string: 
$clean_string = mysql_real_escape_string($query, $db_connection); 
$rs = mysql_query($clean_string, $db_connection); 
if (!$rs) 
{ 
    echo "Invalid input!"; 
} 

This is ALWAYS giving me the

"Invalid input!"

error.

When I take out the clean_string part and just run mysql_query on query, the

"invalid input"

message is not output. Rather, when I do this:

$rs = mysql_query($query, $db_connection); 
if (!$rs) 
{ 
   echo "Invalid input!"; 
} 

It does NOT output

"invalid input".

However, I need to use the mysql_real_escape_string function. What am I doing wrong?

Update:

Given select * from Actor; as an input, I've found the following.

Using echo statements I've found that before sanitizing, the string holds the value: select * from Actor; which is correct. However, after sanitizing it holds the incorrect value of select *\r\nfrom Actor;, hence the error message. Why is mysql_real_escape_string doing this?

like image 842
Casey Patton Avatar asked Apr 17 '11 04:04

Casey Patton


People also ask

What is MySQL_real_escape_string() in MySQL?

mysql_real_escape_string () calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: x00, n, r, , ', " and x1a. This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.

What is the real_escape_string () function in PHP?

Definition and Usage. 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. This function is used to create a legal SQL string that can be used in an SQL statement. Assume we have the following code: <?php.

How do I escape special characters in a string in MySQL?

The mysqli_real_escape_string () function is used to escape characters in a string, making it legal to use in an SQL statement. This is an object representing a connection to MySQL Server. This is a string in which you need to escape the special characters.

What is mysqli_real_escape_string () in MySQL?

The mysqli_real_escape_string () function is used to escape characters in a string, making it legal to use in an SQL statement. This is an object representing a connection to MySQL Server. This is a string in which you need to escape the special characters. The mysqli_real_escape_string () returns a legal string which can be used with SQL queries.


3 Answers

use it on the actual values in your query, not the whole query string itself.

example:

$username = mysql_real_escape_string($_POST['username']);
$query = "update table set username='$username' ...";
$rs = mysql_query($query);
like image 56
Crayon Violent Avatar answered Sep 28 '22 15:09

Crayon Violent


Rather than using the outdated mysql extension, switch to PDO. Prepared statement parameters aren't vulnerable to injection because they keep values separate from statements. Prepared statements and PDO have other advantages, including performance, ease of use and additional features. If you need a tutorial, try "Writing MySQL Scripts with PHP and PDO".

like image 36
outis Avatar answered Sep 28 '22 16:09

outis


mysql_real_escape_string() is the string escaping function. It does not make any input safe, just string values, not for use with LIKE clauses, and integers need to be handled differently still.

An easier and more universal example might be:

 $post = array_map("mysql_real_escape_string", $_POST);
 // cleans all input variables at once

 mysql_query("SELECT * FROM tbl WHERE id='$post[id]' 
                OR name='$post[name]' OR mtime<'$post[mtime]' ");
 // uses escaped $post rather than the raw $_POST variables

Note how each variable must still be enclosed by ' single quotes for SQL strings. (Otherwise the escaping would be pointless.)

like image 30
mario Avatar answered Sep 28 '22 16:09

mario