tags:

views:

82

answers:

3

my query written in php is:

$sql="SELECT * FROM ".TABLE_PREFIX."login WHERE username ='".$username."' 
      AND password='".$password."'";
$res_id = mysql_query($sql);
$num_rows = mysql_num_rows($res_id);
echo $num_rows;

When i enter a valid user name and password from a form it works ok.

When i input some sql injection code the query output is:

SELECT * FROM form_login WHERE username ='' or '1'='1' AND password='' or '1'='1'

Which is a valid sql statement.But it gives an output(ie number of rows) as 0(zero).

If I write the same output sql statement in the program itself as-

$sql="SELECT * FROM form_login WHERE username ='' or '1'='1' 
   AND password='' or '1'='1'";

it works fine and it gives some result(for eg 3).

How can i get the correct result by inputing the sql injection code?

A: 

Before using any variable in a query, pass it through mysql_real_escape_string.

Sjoerd
I think in this case, he WANTS to have a SQL-Injection ;)
DrColossos
Not "any variable", but variable, enclosed in quotes only. So, there must be something for the rest.
Col. Shrapnel
+1  A: 

Try echoing out the SQL statement. It may not be what you think it is, especially if magic_quotes_gpc is enabled.

Amber
i echoed the sql statement and my sql statement is as i have given in the question.SELECT * FROM form_login WHERE username ='' or '1'='1' AND password='' or '1'='1' but the result is not comming.
A: 

Or better yet, NEVER CONCATENATE SQL!

Try using the PDO library with prepared sql statements

Values are inherently safe in this mode.

Justin Alexander