-1

Fatal error: Uncaught Error: Call to a member function prepare() on null in X:\x\x\x\class\auth.php:29 Stack trace: #0 x:\x\x\index.php(8): Auth->register_user() #1 {main} thrown in x:\x\x\x\class\auth.php on line 29

This is database connection file

class getDB {
    protected $conn;

    public $db_host = 'localhost';
    public $db_user = 'root';
    public $db_pass = '';
    public $db_name = 'f_base';

    public function getConn() {
        try {
            $this->conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);

            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        } catch ( PDOException $e ) {
            die ('<h1>ERROR:</h1><h2>'.$e->getMessage().'</h2>');
        }
    }
    // TODO: Dodati funkcije koje ce moci da ucitaju databazu da bi se prikazalo nesto iz databaze 
}

this class for registration

class Auth {

    public function __construct () {
        $connection = new getDB();
        $this->conn = $connection->getConn();

        return $this->conn;
    }


    public function register_user ( $username, $email, $password, $r_date ) {
        $reg_user = $this->conn->prepare("INSERT INTO korisnici ( username, email, password, r_date ) VALUES ( ?, ?, ?, ? )"); // THIS IS LINE I AM GETTING ERROR
        $reg_user->execute( array( $username, $email, $password, $r_date ) );        
    }
}

this line where I am getting error

 $reg_user = $this->conn->prepare("INSERT INTO korisnici ( username, email, password, r_date ) VALUES ( ?, ?, ?, ? )"); // THIS IS LINE I AM GETTING ERROR
yivi
  • 42,438
  • 18
  • 116
  • 138
  • What values are allowed in your table? Check if the data types of the values you are inserting match. – luek baja Jun 25 '20 at 13:40
  • Does this answer your question? [Set PDO to throw exceptions by default](https://stackoverflow.com/questions/8992795/set-pdo-to-throw-exceptions-by-default) – yivi Jun 25 '20 at 13:41
  • `Call to a member function prepare() on null` would be the first clue. ON NULL, meaning `$this->conn` is not a db object. – IncredibleHat Jun 25 '20 at 13:41
  • Your connection fails, but does not throw an exception so you never catch the problem. Then you attempt to call `prepare()` on a `null` connection. – yivi Jun 25 '20 at 13:42

1 Answers1

1

You are making a simple and common mistake. Check you class getDB method getConn() - you are not returning anything that is way you are getting error message "prepare() on null". You are trying to get PDO connect object in Auth class construct $this->conn = $connection->getConn();

Just add return on like this

class getDB {
protected $conn;

public $db_host = 'localhost';
public $db_user = 'root';
public $db_pass = '';
public $db_name = 'f_base';

public function getConn() {
    try {
        $this->conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);

        $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    } catch ( PDOException $e ) {
        die ('<h1>ERROR:</h1><h2>'.$e->getMessage().'</h2>');
    }
    return $this->conn;
}
// TODO: Dodati funkcije koje ce moci da ucitaju databazu da bi se prikazalo nesto iz databaze 

}

mail2bapi
  • 1,547
  • 1
  • 12
  • 18