I'm trying to create a function where I can simply do <?php echo $user_args['user_id']; ?> to call a variable. If a user is logged in I can use this format. I can't get this function working in PDO though.
I'm getting this error message:
Notice: Undefined variable: db in /Applications/XAMPP/xamppfiles/htdocs/app/user/func/user.func.php on line 35
    Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/app/user/func/user.func.php on line 35
This is the function I'm trying to do:
$db = new PDO("mysql:host=$servername; dbname=$database", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function user_args() {
        $user_id = $_SESSION['user_id'];
        $args = func_get_args();
        $fields = implode(', ', $args);
        $query = $db->prepare("SELECT * FROM users WHERE user_id = :user_id");
        $query->bindParam(':user_id', $user_id);
        if($query->execute()) {
            $query_success = $query->fetch(PDO::FETCH_ASSOC);
            foreach($args as $arg) {
                $args[$arg] = stripslashes($query_success[$arg]);
                }
            return $args;
            }
        }
    $user_args = user_args('user_id',
                            'username',
                            'email',
                            'password');
    echo $user_args['user_id']; // <- The function isn't working so I can't do this
What's wrong in my code that's making this not work? Thanks!
 
    