-1

This is my code and I am facing this error

Call to a member function prepare() on null in I think my classes are alright but there is an error and I don't know how to make this code running

<?php
/*$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "motorway";*/

class BaseConnect {
    private $conn;

    function __construct() {
    }

    // Create connection
    function connect() {
        $conn = new mysqli("localhost:3307", "root", "", "motorway");
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        return $this->conn;
    }
}
class Database {
    private $conn;

    function __construct() {
        $db = new BaseConnect();
        $this->conn = $db->connect();
    }

    function Enterdata($name, $phone, $email, $password) {
        $pass = md5($password);
        $sql = $this->conn->prepare("INSERT INTO 'user'('name', 'phone', 'email', 'pass')
    VALUES(?, ?, ?);");
        $sql->bind_param("ssss", $name, $phone, $email, $pass);
        if ($sql->execute()) {
            return 1;
        } else {
            return 2;
        }
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 3
    `return $this->conn` should be `return $conn` – Dharman Dec 04 '20 at 13:15
  • 1
    A later issue will be that your SQL is invalid. Don't quote identifiers, and you have 4 columns defined but only 3 values going in `VALUES(?, ?, ?)`. Also `md5` shouldn't be used, https://www.php.net/manual/en/function.password-hash.php – user3783243 Dec 04 '20 at 13:15
  • 1
    Please note that `BaseConnect` class is completely redundant and you should remove it. You are also not using mysqli correctly. Please enable error reporting. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Dec 04 '20 at 13:16
  • 1
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Dec 04 '20 at 13:20

1 Answers1

-2

If you wanna use private $conn member, you have to change this code:

$conn = new mysqli("localhost:3307", "root", "", "motorway");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

with this:

$this->conn = new mysqli("localhost:3307", "root", "", "motorway");
// Check connection
if ($this->conn->connect_error) {
    die("Connection failed: " . $this->conn->connect_error);
}

Anyway BaseConnect class is totally redundant, you should delete it and use Database class.

Giacomo M
  • 4,450
  • 7
  • 28
  • 57
  • 3
    Please don't post answers only pointing out a typographical issue or a missing character. Such answers are unlikely to help future visitors since they are specific to OP's code. Instead, flag or vote to close the question as off-topic as per the [help/on-topic]. – Dharman Dec 04 '20 at 13:20
  • its not about typographical issue. As I explained (and you didn't read I guess), I specified that since he is using a class member, he has to use $this. And this is an OP issue. – Giacomo M Dec 04 '20 at 13:21
  • @GiacomoM The typo is the usage of `$this`. Relative variable is fine just `return` must be relative as well. In later code has `$this->conn = $db->connect()` which would get the local connection returned. – user3783243 Dec 04 '20 at 13:49
  • even in this sense it's obviously a duplicate – Your Common Sense Dec 04 '20 at 13:55
  • @YourCommonSense it's a duplicate, but you still could answer to the question (and there is no reason to downvote it). Cause its not a typo. Its clearly an OP issue, since the user does not know the difference between a local variable and a member variable. – Giacomo M Dec 04 '20 at 14:57