开发者

Mysql statement inside a function doesn't work

I have a MySQL statement in a function but somehow it doesn't work. I get the following error:

Fatal error: Call to a member function prepare() on a non-object in D:\xampp\test.php on line 7

function isloggedin($getcookiedata) {
    $data = explode("|", $getcookiedata);
    $userid = $data[0];
    $md5password = $data[1];

    $sql = "SELECT user_id, username, email, newsletter, user_group FROM users WHERE user_id = ? AND md5password = ?";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('ss', $userid, $md5password);
    $stmt->execute();
    $stmt->bind_result($r_userid, $r_username, $r_email, $r_newsletter, $r_user_group);
    $stmt->store_resul开发者_Go百科t();
    $checker = $stmt->num_rows;
    $stmt->fetch();
    $stmt->close(); 
}

When I just do the SQL statement outside a function, it works. But not inside the function.


If $mysqli is a global you're defining somewhere, you need to pull it into your function's scope with:

function isloggedin($getcookiedata) {
    global $mysqli;
    ...
}

See PHP - Variable scope

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜