Jump to content

Do I need to use mysql_real_escape_string in SELECT queries?


php_guest

Recommended Posts

Suppose you have a query to check for user login

<?php
$query = "SELECT ID FROM users WHERE username = '{$_POST['username']}' AND password = MD5('{$_POST['password']}')";
$result = mysql_query[$query];
if(mysql_num_rows($result) > 0) {
  //do login stuff
  echo "You're now logged in";
} else {
  echo "Login incorrect, try again.";
}

 

Now let's say I use the login form to send a username like this:

foo' OR 1; --

 

So the query sent to database will look like this:

SELECT ID FROM users WHERE username = 'foo' OR 1;--' AND password = MD5('') 

 

(-- starts a comment in MySQL, just like // starts comment in PHP - everything after -- is ignored by database)

This will in turn return all rows from users table, so mysql_num_rows() will return a number larger than 0 and the login will be successful

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.