douzhouqin6223 2015-09-05 10: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 10: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.

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号