在PHP类之外的索引中使用var [关闭]

How can i acces the $pos var outside a class? so i can use it in my index like: echo $pos i also icluded this class in my index.

public function CheckProfile(){

    $get = $this->db->query("SELECT positive, posts FROM users WHERE user='". $_SESSION['user'] ."'");

    while($row = $get->fetch(PDO::FETCH_ASSOC))
        {
            return $pos = $row['post'];
        }

    }

As everybody told you, there are many wrong things going on in your code. You should learn how to use Classes and Objects better. Anyways, here is something to start with:

Assuming you have a Class, let's define a public method:

public function CheckProfile($user) {

    $ret = false;

    $stmt = $this->db->prepare("SELECT positive, posts FROM users WHERE user = :user");
    $stmt->bindParam(':user', $user, PDO::PARAM_STR);

    if ($stmt->execute())   {
        $ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    return $ret;
}


Then, from your php file, use it like that:

$UserData = $YourClassInstance->CheckProfile($_SESSION['user']);
if ($UserData === false) {
    echo 'Something went wrong...';
} else {
    print_r($UserData);
}

Start from this and try to accomplish what you really need.