Fatal error: "Call to a member function prepare() on a non-object"

When I run the login it gives this error:

Fatal error: Call to a member function prepare() on a non-object in /home/lankaf5/public_html/admin/admin/functions.php on line 30

I have marked the line 30 that it's referring to; can someone please check and tell what's wrong? I tried all possible ways, but can't figure it out.

// Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); function login($email, $password, $conn) {; $sql ="SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1"; // Using prepared statements means that SQL injection is not possible. if ($stmt = $conn->prepare($sql)) { // line 30 that the error is referring to $stmt->bind_param('s', $email); // Bind "$email" to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($user_id, $username, $db_password, $salt); $stmt->fetch(); // hash the password with the unique salt. $password = hash('sha512', $password . $salt); if ($stmt->num_rows == 1) { // If the user exists we check if the account is locked // from too many login attempts if (checkbrute($user_id, $conn) == true) { // Account is locked // Send an email to user saying their account is locked return false; } else {

-------------Problems Reply------------

Make this fix first:

function login($email, $password, $conn) {; // <-- remove the semi-colon

Now add this adjustment:

function login($email, $password, mysqli $conn) {

This will force the connection to be a mysqli class, and on the occasions it is not set, it will throw a fatal error closer to the source of the problem.

You will still need to find out why this is failing. I suspect your database credentials are incorrect, or perhaps you have not set a connection when you call this function at all?

Category:php Views:4 Time:2015-01-11
Tags: php mysqli

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.132 (s). 11 q(s)