Skip to main content
Participating Frequently
September 14, 2012
Question

Login system pager error?

  • September 14, 2012
  • 1 reply
  • 1204 views

I've created a basic PHP login system as a personal project. I've finished all the pages but when I try and submit a username and password I get an error :

Fatal error: Call to a member function prepare() on a non-object in /home/(my username)/public_html/classes/Mysql.php on line 20

I've tinkered with it for a while but I haven't been able to find a solution, can anyone help me out?

<?php

require_once'includes/constants.php';

class Mysql {

          private $conn;

 

          function __contruct() {

                    $this->conn = new mysql (DB_SERVER, DB_USER, DB_PASSWORD,DB_NAME) or

                              die('Problem connecting to database');

          }

 

          function verify_Username_and_Pass($un, $pwd) {

 

                    $query = "SELECT *

                                        FROM users

                                        WHERE username = ? AND password = ?

                                        LIMIT 1";

 

                    if($stmt = $this->conn->prepare($query)) {

                              $stmt->bind_param('ss', $un, $pwd);

                              $stmt->execute();

 

                              if($stmt->fetch()) {

                                        $stmt->close();

                                        return true;

                              }

 

                    }

 

          }

}

This topic has been closed for replies.

1 reply

sudarshan.t
Inspiring
September 15, 2012

Can you give us an idea of your file hierarchy?

Apart from that, the first thing I noticed is you're missing ()brackets in your require statement.

Try require_once('includes/constants.php');

David_Powers
Inspiring
September 15, 2012

Sudarshan Thiagarajan wrote:

Apart from that, the first thing I noticed is you're missing ()brackets in your require statement.

Try require_once('includes/constants.php');

That makes no difference. The parentheses are optional with include and require.

There are a couple of problems in the class definition here:

function __contruct() {

                    $this->conn = new mysql (DB_SERVER, DB_USER, DB_PASSWORD,DB_NAME) or

                              die('Problem connecting to database');

          }

__construct() is misspelled.

Also it should be new mysqli.

sudarshan.t
Inspiring
September 15, 2012

That makes no difference. The parentheses are optional with include and require.

Gosh, What was I thinking?! Thanks David for the correction. Appreciate it!

Sorry, Tyharo - if I misguided you.