I am trying to echo or print the last value of column usercode using PHP PDO. I tried to do this by using name column and SESSION var which will be the last values as references, but it doesn't work.
$name = $_SESSION['name'];
$query = $db->prepare("SELECT usercode from users where name = $name ");
$query->execute();
$result = $query->setFetchMode(PDO::FETCH_ASSOC);
echo $result;

setFetchModesets a mode. It does not fetch any result.$name. But you should use bound parameters, then that's not a problem.