PDO user_data函数[关闭]

So in my init.php file it checks to see if the user is logged in, that requires a function called user_data($username)

Here's what they contain.

init.php

if (logged_in() === true) {
    $session_user_id = $_SESSION['user_id'];
    $user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last_name', 'email', 'coins', 'host', 'bets', 'online', 'multi');
    $user_id = $user_data['user_id'];   
    if (user_active($user_data['username']) === false) {
        session_destroy();
        header('Location: index.php');
        exit();
    }
}

The function user_data() is

function user_data($user_id){
$data = array();
$user_id = (int)$user_id;

$func_num_args = func_num_args();
$func_get_args = func_get_args();

if ($func_num_args > 1) {
    unset($func_get_args[0]);

    $fields = '`' . implode('`, `', $func_get_args) . '`';
    $sql = sprintf('SELECT `%s` FROM `users` WHERE `id` = ? LIMIT 1', $fields);
    $stmt = $dbh->prepare($sql);
    $stmt->execute(array($user_id));
    $data = $stmt->fetch(PDO::FETCH_ASSOC);

    return $data;
}
}

I get the error PHP Fatal error: Call to a member function prepare() on a non-object is this because I'm not calling the function with $db or is my function just incorrect?

You haven't connected to the database. You should have a PDO database connection inside the user_data function, or global, or pass it in.