As my process is almost complete for rewriting web with PDO instead of mysql_* commands I am now testing my changed functions. And It seems that my changed function for mysql_result(mysql_query() always returns true, why is that? Lets see original and changed code:
if (mysql_result(mysql_query("SELECT COUNT(*) FROM account WHERE id='".$_SESSION["user_id"]."' AND online=1"), 0)>0)
{
return true;
}
else
return false;
And changed code here:
$stmt = $db_login->prepare("SELECT COUNT(*) FROM account WHERE id=:id AND online=1");
$stmt->bindValue(':id', $_SESSION["user_id"], PDO::PARAM_INT);
$stmt->execute();
$results_login = $stmt->fetch(PDO::FETCH_ASSOC);
$rows = count($results_login);
if ($rows > 0)
{
return true;
}
else
return false;
So what is wrong with is why it always returns true even when column has online=0? Thank you
$stmt->fetchfetches one row from the result set. What you get out of that is an array containing all the selected columns, looking something like this:A
count()on that array will always result in1.You need to check the contents of the fetched row:
It’s best to alias this column to a nicer name:
Then: