douzhouqin6223 2015-09-05 02:18
浏览 20
已采纳

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

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

展开全部

  • 写回答

1条回答 默认 最新

  • doujiao1981 2015-09-05 02:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部