试图在PHP PDO中获取非对象的属性

时间:2015-09-05 10:18:26

标签: php oracle pdo

我在php中的登录页面中使用了以下函数。它显示错误

  

“试图获取非对象的属性......”

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;

    }

任何人都可以帮助我..我是php的新手..请帮忙

提前致谢

1 个答案:

答案 0 :(得分:0)

字符串不是对象:

$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

$sqlstring,因此它没有error等属性,您无法像$sql->error那样访问它,因为它是原始值类型。

您必须删除此行:

echo $sql->error;

要处理PDO中的SQL错误,必须在PHP中使用try-catch构造。 See this answer for more information about handling errors in PDO.