uncaught error: call to a member function prepare on null

// database code

<?php
//global $this->conn;
class Database{
private $host = “localhost”;
private $db_name = “users”;
private $username = “root”;
private $password = “No”;
public $conn;

public function getConnection() {
// $this->conn = null;
try{
$this->conn = new PDO(“mysql:host=” .$this->host . “;dbname=” . $this->db_name , $this->username, $this->password);
$this->conn->exec(“set names utf8”);
}catch(PDOException $exception){
echo “Connection error: ” . $exception->getMessage();
}
return $this->conn;
}
}
?>

//user.php‘s code

// the error comes in signup function and isAlreadyExists funciton()

<?php
class User{
private $conn;
private $table_name = ‘user’;
//object properties
public $id;
public $username;
public $password;
public $created;
public function __constuct($db){
$this->conn = $db;
}
function signup(){
if($this->isAlreadyExist()) {
return false;
}
$query = “INSERT INTO
” . $this->table_name . “
SET
username=:username,
password=:password,
created=:created”;
//prepare query
$stmt = $this->conn->prepare($query);
//sanitize
$this->username=htmlspecialchars(strip_tags($this->username));
$this->password= htmlspecialchars(strip_tags($this->password));
$this->created=htmlspecialchars(strip_tags($this->created));
//bind values
$stmt->bindParam(“:username”, $this->username);
$stmt->bindParam(“:password”, $this->password);
$stmt->bindParam(“:created”, $this->created);
//execute query
if($stmt->execute()) {
$this->id = $this->conn->lastInsertId();
return true;
}
return false;
}
function login(){
$query = “SELECT

‘ id’
,
‘username’,
‘password’,
‘created’
FROM
” . $this->table_name . “
WHERE
username='”.$this->username.”‘
AND password='”.$this->
password.”‘”;
$stmt = $this->conn->prepare($query);
//execute query
$stmt->execute();
return $stmt;
}
function isAlreadyExist(){
$query = “SELECT *
FROM
” . $this->table_name .”
WHERE
username=’ “.$this->username.”‘”;
$stmt = $this->conn->prepare($query);
$stmt->execute();
if($stmt->rowCount()> 0) {
return true;
}else{
return false;
}
}
}
?>

please help me where to make changes.

I also have other two files named signup.php and login.php but whenn i paste the link of the class user in postman to work with that; I am getting this error.

I am new to php pdo please help and guide me.

submitted by /u/Comfortable_Pound267
[link] [comments]