<?php
$con = mysql_connect('localhost','root','');

<?php
    $con = mysql_connect('localhost','root','');
    $db = mysql_select_db('myuploads',$con);$un = $_GET['usname'];
    $ps = $_GET['passwd'];
    $retr = mysql_query("SELECT uname,password FROM user WHERE uname = '$un' ");
    while($row = mysql_fetch_assoc($retr)) {
        if($row['uname'] == $un) {
            if($row['uname']== $un && $row['password'] == $ps) {
                echo "Welcome ".strtolower($un);
            }
            else
                echo "Password doesnt match";
        }
        else
            echo "User ".$un." doesnt exist";
    }
?>

Recommended Answers

All 6 Replies

it does not get into the final else statement, nothing gets displayed, when i enter a username that is not present in the database, not even a warning or error

Hello 'viveksraman'.

You have to debug this by echoing before and after the conditional statements, and by adding the "or die(mysql_error());" after a function (check later in my code).

Or try to put this $row = mysql_fetch_assoc($retr); just before the while.

Anyway you can add this to your code
$row_num = mysql_num_rows($retr);
this will return the number of rows found in the database, if it's 0 than nothing found, if you want the username to be unique then the variable should be equal to one and NOT greater than one!!

Here's how I write your idea:

$connection = mysql_pconnect("localhost","root","password") or die(mysql_error());
$database = "amirbwb";

$un = $_GET['usname'];
$ps = $_GET['passwd'];

mysql_select_db($database , $connection) or die(mysql_error());
$query = mysql_query("SELECT uname,password FROM user WHERE uname = '$un' AND password = '$ps'");
$row = mysql_fetch_assoc($query);
$num_rows = mysql_num_rows($query);

//I considered the user is unique, if not change '== 1' to '> 0'
if($num_rows == 1){
    echo "Hello " . $row['uname'];
}else
{
    echo $un." not found";
}

PS: THE CODE IS WRITTEN DIRECTLY HERE AND NOT TESTED

I just noticed that you want the option, Password doesn't match. OK
Just replace this in my previous code:
I recommend you to try both codes cuz you may use the first code block in your future ;)

mysql_select_db($database , $connection) or die(mysql_error());
$query = mysql_query("SELECT uname,password FROM user WHERE uname = '$un'");
$row = mysql_fetch_assoc($query);
$num_rows = mysql_num_rows($query);

//I considered the user is unique, if not change '== 1' to '> 0'
if($num_rows == 1){
    if($ps != $row['password']){
        echo "Password doesn't match";
    }else
    {
        echo "Welcome " . $row['uname'];
    }
}else
{
    echo $un." not found";
}

Good Luck
PS: THE CODE IS WRITTEN DIRECTLY HERE AND NOT TESTED

thanks for the reply bro, but i hav a question, what is the problem with my last else statement, it does no execute, when i enter a username tha is not been listed in my database. look at the seventh line of my code, if that coondition is not satisfying then the control must have jumped over to fourteenth line, but i get nothing, when my if condition gets wrong, y so ?

Hi amir bro,i don't get any type of bug or anything. if i enter a username that is ther in my db it works prettygood, even while checking whether the username and password belongs to the same row, it works fine, but i dont get anything when i enter a username that is not listed in my db, with my coding, i expect to print "User doesnt exist", i will be glad if i get the root cause for this, am a newbie to PHP :)

Hello again 'viveksraman', the problem is that you are doing a while loop in which the first condition is false, in other words
while($row = mysql_fetch_assoc($retr))
is the same as
while(false) which will not read the content and will directly jump to the last close curly brkt

while($row = mysql_fetch_assoc($retr))
is only used if you are sure you have more than 1 row as a result of you query "SELECt ..."

since here there is no result, mysql_num_rows($retr) = 0;
$row = mysql_fetch_assoc($retr) is $row = false
therefor while(false) and the body is not executed

I want to repeat one thing, in checking username and password, in most cases we don't use while, cuz username are unique, but it's not a problem if you use it :)
to fix your code

SOLUTION OF YOU CODE:
1 - Back to your code, remove the else that is not executing and will never.
2 (First Method) - And you can add a boolean variable like found = false; before the while loop.
Inside the while loop write found = true;
and after exiting the while loop, write this condition

if(!found)
    echo $un . " not found";

2 (Second Method) - you can simply add this after the exiting the while loop:

if(mysql_num_rows($retr) == 0)
    echo $un . " not found";

Hope I was clear in my explanation :)
Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.