1

So just trying to make a simple register/login form which i have done before, but not for hell can i figure out why this is happening. my code looks like the below.

Ive commented out the error above the line that it says its occurring.

<?php 

class connection{

    private $hostname = "localhost";
    private $username = "root";
    private $password = "";
    private $database = "test";
    private $conn;

    public function __construct(){
        $conn = new mysqli($this->hostname, $this->username, $this->password, $this->database)or die("MySQL Connection Error");
    }
    public function getConn(){
        return $this->conn;
    }
}
class queries{

    private $conn;

    public function __construct($conn){
        $this->conn = $conn;
    } 

    public function checkUser($username, $email){
        $query = "SELECT * FROM users WHERE username = '$username' OR email = '$email'";

//Call to  a member function query() on null in C:\xampp\htdocs\i\functions.php on line 28

        $result = $this->conn->query("$query");
        return $result;
    }

    public function insertUser($activated, $activation_code, $firstname, $lastname, $username, $password, $email){
        $query = "INSERT INTO users (activated, activation_code, firstname, lastname, username, password, email)
                  VALUES ('$activated', '$activation_code', '$firstname', '$lastname', '$username', '$password', '$email')";
        $result = $this->conn->query("$query");
    }
}

Seems Simple enough right.. now heres my php code on the page thats being used (register.php).

if (isset($_POST['register'])) {

    include 'functions.php';

    $connection = new connection();
    $query = new queries($connection->getConn());

    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname  = mysql_real_escape_string($_POST['lastname']);
    $username  = mysql_real_escape_string($_POST['username']);
    $email     = mysql_real_escape_string($_POST['email']);
    $password  = sha1($_POST['password']);

    $user = $query->checkUser($username, $email);

    if ($user->num_rows > 0) {
        echo "User Exists";
    }else{
        echo "User Does not Exist";
    }
}
Kenziiee Flavius
  • 1,918
  • 4
  • 25
  • 57

1 Answers1

3

Your code seems correct, except in the constructor of your connection class, you've forgot to use the $this, change it to something like the following:

public function __construct() {
    $this->conn = new mysqli($this->hostname, $this->username, $this->password, $this->database)or die("MySQL Connection Error");
}
deceze
  • 510,633
  • 85
  • 743
  • 889
someOne
  • 1,975
  • 2
  • 14
  • 20