Jump to content

HELP : mysql_real_escape_string


RON_ron

Recommended Posts

This code gives an error. Please help fix.

 

$mydb = mysql_connect("localhost","my_un","my_pw");
mysql_select_db("my_db");

$query =sprintf("SELECT * FROM  idb1 WHERE username = '%s' AND authority = 'Banned'", mysql_real_escape_string($userNm));
if(mysql_num_rows($query)) {
$login = "&err=Not allowed.";
echo($login);
} else {

$result=sprintf("SELECT * FROM  idb1 WHERE username = '%s' AND password ='%s'", mysql_real_escape_string($userNm), mysql_real_escape_string($passWd));
if(mysql_num_rows ($result) == 0) {
$login = "&err=Retry!!";
echo($login);
} else {
$row = mysql_fetch_array($result);
$userNm=$row['username'];
$passWd=$row['password'];
$login = "$userNm=" . $userNm . "$passWd=" . $passWd . "&err=Successful.";
echo($login);
}
}

Link to comment
Share on other sites

Actually, don't bother. Here....

 

$result=sprintf("SELECT * FROM  idb1 WHERE username = '%s' AND password ='%s'", mysql_real_escape_string($userNm), mysql_real_escape_string($passWd));
if(mysql_num_rows ($result) == 0) {

 

$result is a string. You never actually execute the query.

Link to comment
Share on other sites

Thanks Thorpe!

Oops! I forgot to show the error... Here goes.

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/.../test.php on line 12

&err=Retry.

if(mysql_num_rows ($result) == 0) {

 

After I included "mysql_real_escape_string", it doesn't allow me to login (even with legitimate usernames and passwords). Have I done something wrong? or What might be the issue here?

Link to comment
Share on other sites

oh! Ok. I'm not entirely sure If i understood. You mean the "mysql_real_escape_string" shouldn't be applied here?

 

$result=sprintf("SELECT * FROM  idb1 WHERE username = '%s' AND password ='%s'", mysql_real_escape_string($userNm), mysql_real_escape_string($passWd));
if(mysql_num_rows ($result) == 0) {

 

$$result=sprintf("SELECT * FROM  idb1 WHERE username = '$userNM' AND password ='$passWd'");
if(mysql_num_rows ($result) == 0) {

Link to comment
Share on other sites

Okay so far i saw is you are directly using a variable without assigned.

$variable = mysql_real_escape_string($_POST['YOUR_FORM_USER_NAME_TEXT']);  You are assigning value of User name which comes from your post into a variable. So, when your code keep running on next line you will have user entered plain text, stripped out any dangerous code inside. Then put this plain text into your sql query.

 

$query =sprintf("SELECT * FROM  idb1 WHERE username = '%s' AND authority = 'Banned'", $variable);

 

But, i can not see your whole page code and this all i do my best.

 

Let us know if you have any further problem.

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.