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
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.