dongyu9263 2013-05-10 15:23
浏览 33
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dongshuofu0039 2013-05-10 15:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换