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.

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

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案