Jump to content

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
https://forums.phpfreaks.com/topic/220330-help-mysql_real_escape_string/
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.

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?

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) {

Why dont you get a variable out of mysql query and then put that variable into query?

$variable = mysql_real_escape_string($userNm);

 

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

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.

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.