0

Following function I used in a login page in php. Its showing a error that

" Trying to get property of non-object in...."

function Validate($userName,$encrypted_password,$dbh)
    {
        try{
            echo ("".$userName."");
            echo ("".$encrypted_password."");
            $sql = "SELECT USERCODE,PWD FROM GUSER WHERE USERCODE = :uname AND PWD = :pwd";
            echo $sql->error;
            $query = $dbh->prepare($sql);
            $query->bindParam(':uname',$userName,PDO::PARAM_STR);
            $query->bindParam(':pwd',$encrypted_password,PDO::PARAM_STR);
            $query->execute();
            $rows = $query->fetch(PDO::FETCH_NUM);
            echo ("".$rows."");
            if($rows > 0){
                echo "Login Successfull";
                header("location: home.php");
            }
            else{
                $errmsg_arr[] = 'Username and Password are not found';
                $errflag = true;
            }
            if($errflag) {
            $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
            session_write_close();
            echo "Closed";
            exit();
            }
        }                   
        catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
        //return $getValue;

    }

Can anyone help me out.. I am new to php.. Please help

Thanks in advance

Saty
  • 22,443
  • 7
  • 33
  • 51
Vipin KA
  • 33
  • 1
  • 12

1 Answers1

0

String is not an object:

$sql = "SELECT USERCODE,PWD FROM GUSER WHERE USERCODE = :uname AND PWD = :pwd"; // STRING
echo $sql->error; // but you try to access it like it's an object

$sql is string so it doesn't have properties like error and you can't access it like $sql->error, because it's a primitive value type.

You have to delete this line:

echo $sql->error;

To handle SQL errors in PDO you have to use try-catch construct in PHP. See this answer for more information about handling errors in PDO.

Community
  • 1
  • 1
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • Its only printing "Closed" that specified in last if statement. Any error with my query? – Vipin KA Sep 05 '15 at 10:28
  • You have to delete line: `echo $sql->error;`. Do this and error is fixed. – Daniel Kmak Sep 05 '15 at 10:29
  • i deleted. then i ran my code in browser. its printing "sysman 1594 Closed" only. – Vipin KA Sep 05 '15 at 10:34
  • So error is gone. You were asking about this error only. How your database structure like? Maybe you should have `USER` table name instead of `GUSER`? How this table look like and what's it name in MYSQL? – Daniel Kmak Sep 05 '15 at 10:38
  • It's is not finding the user because you are printing Closed when the $errflags variable is set to true. So for some reason it is not finding the user -password combination in the DB. – Mark C. Sep 05 '15 at 10:45
  • Table name is GUSER. Its a oracle database – Vipin KA Sep 05 '15 at 10:59
  • Are you sure you use correct username and password? I guess you have 0 rows and login fails and that's what you're asking. – Daniel Kmak Sep 05 '15 at 11:02