2

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_result();
    $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.

1
  • Please add your solution as an answer instead (yes you can answer your own questions). In about two days you can accept it then as "the answer". Commented Jul 9, 2011 at 10:27

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.