Jump to content

[SOLVED] Problem with MySQLi binding: call to member function bind_param() on non-object


cgm225

Recommended Posts

I have the following class and when I use it I get the following error: "Fatal error: Call to a member function bind_param() on a non-object in C:\public_html\DEVELOPMENT\authentication.php5 on line 26"

 

Is this a problem with my MySQLi binding?

 

Thanks in advance!  Any other feedback is greatly appreciated!

 

$mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD);


class Authentication {
    
    //Declaring variables
    private $username;
    private $password;
    private $connection;

    //Setting username and password
    public function __construct($username, $password) {
        $this->username = $username;
        $this->password = md5($password);
    } 

    public function doLogin($connection, $database, $table, $usernameField, $passwordField) {
        $connection->select_db($database);
        $statement = $connection->prepare("SELECT COUNT(*) FROM '$table' WHERE '$usernameField' = ? AND '$passwordField' = ?");
        $statement->bind_param('ss', $this->username, $this->password);
        $statement->execute();
        $statement->bind_result($count);
        $statement->fetch();
        if ($count == 1) {
            $this->setSession($this->username, $this->password);
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    //Setting the provided username and password to session variables
    public function setSession($username, $password) {
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
    }
}


$authentication = new Authentication("user1", "pass1");
$authentication->doLogin($mysqli, '_authentication', 'users', 'username', 'password');

$mysqli->close();

Link to comment
Share on other sites

i think  the problems with the prepare, try this

update

       $statement = $connection->prepare("SELECT COUNT(*) FROM '$table' WHERE '$usernameField' = ? AND '$passwordField' = ?");

to

       $statement = $connection->prepare("SELECT COUNT(*) FROM '$table' WHERE $usernameField = '?' AND $passwordField = '?' ");

 

Link to comment
Share on other sites

close.. thanks for getting me on the right track

 

it needed to be

 

 

        $statement = $connection->prepare("SELECT COUNT(*) FROM $table WHERE $usernameField = ? AND $passwordField = ? ");
        $statement->bind_param('ss', $this->username, $this->password);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.