ineedhelp
ineedhelp

Reputation: 125

Reason for: Trying to get property of non-object in php

    if(isset($_POST['uname'])) 
    {       
        $query = "SELECT * FROM user_info";
        $res = $mysqli->query($query);
        $num = $res->num_rows;
        $i = 0;     
        $cpwd = $_POST["pswd"];
        $hpwd = SHA1($cpwd);
        $tmp = $_POST["uname"]; 
        while($i < $num)
        {
            $row = $res->fetch_object();
            $name = $row->Username;
            $pwd = $row->Password;  
            if($name == $tmp)
            {       
                   //check if user is blocked
            }
            //hashed pwd
            if($pwd == $hpwd)
            {   
                //success
                exit();
            }   
            //request for pwd change
        else if($pwd == $cpwd)
            {
            //change
                exit();
            }
            else if($pwd != $hpwd)
            {

                //incorrect pwd
            }
        }
        $i++;
    }
    if($i == $num)
    {
        //new user
    }
}

Upvotes: 0

Views: 3892

Answers (4)

Your Common Sense
Your Common Sense

Reputation: 157862

get rid of that junk and make it like this

$query = "SELECT * FROM user_info WHERE Username = ? AND Password = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss', $_POST["uname"], SHA1($_POST["pswd"]));
$stmt->execute() or trigger_error($mysqli->error());
if (!$mysqli->affected_rows) {
  //no such user
}

I've never used mysqli myself, so, there may be typos.
But I hope you'll be able to get the idea.

Upvotes: 1

arnial
arnial

Reputation: 21

some times num_rows return 1, even if no rows effected. Try to use

while($row = $res->fetch_object())

or you forget to increment $i :)

Upvotes: 1

grahamparks
grahamparks

Reputation: 16296

I'd guess that you're somehow looping past the end of the array and $row is actually NULL.

Upvotes: 1

greg0ire
greg0ire

Reputation: 23255

So $res->fetch_object() did not return an object. Take a look at the documentation of this function. What does it return when it finds nothing?

Upvotes: 1

Related Questions