hi all:

i am trying to use this code to login to members area but everytime i try to login with a registered user it redirects to the registration page please help.

below is the code:


$Password = MD5($_POST['Password']);


mysql_connect("$host", "$uname", "$pword")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");

$checkUsername=mysql_query("select Username, Password from registration where Username='Username' and Password='Password'");

$Username_exist=mysql_num_rows ('$checkUsername');

if ($Username_exist>0)
    echo "not registered";

and also how do i destroy a session please help ASAP. thank you

Edited by pranay1995

5 Years
Discussion Span
Last Post by pranay1995

The trouble could be in your query on line 16 where you should use the variables (but you probably only forgot the $ sign). The correct query would be:

$checkUsername=mysql_query("select Username, Password from registration where Username='$Username' and Password='$Password'");

get rid of the ' ' around $checkUsername

There should only be 1 result if the user/pass combo exists. No one will have the same username, right? So change line 20 to:

if($Username_exists == 1) {
 header("Location: indexma.php");
 exit();//all header redirects need to kill the script or it will continue to run even after a redirect
else {
 //echo will not ever be seen, and it should in fact raise an error if something is output before a header change
 header("Location: register.php");
 exit();//same dealy here

EDIT: The above is very possible, as well. But I would encourage my changes as well.

Edited by ryantroop


destroying a session:


as for your problem, on line 16 add:

or die(mysql_error()); and see if your sql is failing.

EDIT: changed cookie name. Spelling error :(

Edited by ryantroop


Let's debug the query first. Put this on line 17 and post the output:

die("select Username, Password from registration where Username='$Username' and Password='$Password'");

( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\wamp\www\Shivam1\login1.php on line 16
Call Stack
#   Time    Memory  Function    Location
1   0.0005  374144  {main}( )   ..\login1.php:0
2   0.0033  382864  mysql_num_rows ( )  ..\login1.php:16
select Username, Password from registration where Username='mo' and Password='30822eb8a7af334741f6867927a8c38f'
$checkUsername=mysql_query("select Username, Password from registration where Username='$Username' and Password='$Password'");

youre code is correct i hope you just forget tu put $ ... :))

Edited by bad.boy.Vlad


my mistake, I forgot $ this time...

echo "$Username_exists"; exit();

Your answer should be a 1 or a 0

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.