Hello i was trying to make function with PDO but getting error (new to PDO) here is my code
function mail_id($mail){
global $host, $dbname, $user, $pass;
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$STH = $DBH->query("select count(from) from messages where from = '$mail'");
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
return $row;
}
i want to count row FROM and here is the out put i was trying
$mail=mail_id($userid);
if (0<count($mail['to_viewed'])) {echo "green";} else {echo "gray";}
this is the error
Call to a member function setFetchMode() on a non-object
please help thx
You need to check the return value of
PDO::query(). In (ugly) PHP there are lots of functions that return a value of mixed type. In the case ofPDO::query()the return type is eitherPDOStatementor bool, although the prototype in the documentation says something different:Looks like it always returns
PDOStatement.Oops, not in every case! Therefore you are not guaranteed that the value returned is a
PDOStatement.The correct prototype would be:
In your case the query is invalid (by using a reserved keyword like FROM as a column name) which results in a return value of type boolean with the value FALSE. A boolean value is not an object and therefore your call to
$STH->setFetchMode()fails.Depending on your PDO::ATTR_ERRMODE you get
return value
errorCode()anderrorInfo()