• PHP Help
  • Fatal error: Uncaught Error: Call to a member function prepare() on null in

i'm still learning about php ,please help me to find the answer for this

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\mvc\app\models\Scamlegit_model.php:20 Stack trace: #0 C:\xampp\htdocs\mvc\app\controllers\scamlegit.php(7): Scamlegit_model->getAllScamlegit() #1 C:\xampp\htdocs\mvc\app\core\App.php(39): scamlegit->index() #2 C:\xampp\htdocs\mvc\public\index.php(5): App->__construct() #3 {main} thrown in C:\xampp\htdocs\mvc\app\models\Scamlegit_model.php on line 20

and this my code

<?php

class Scamlegit_model {
private $dbh;
private $stmt;

public function __contruct(){

	$dsn = 'mysql:host=localhost;dbname=,mvc';

	try{
		$this->dbh = new PDO($dsn,'root','');
	}catch(PDOException $e){
		die($e->getMassage());
	}
}

public function getAllScamlegit()
{
	$this->stmt = $this->dbh->prepare('SELECT * FROM scamlegitinfo');
	$this->stmt->execute();
	return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

}

PLEASE HELP ME 🙁

    1. It's spelled getMessage, not getMassage.

    2. It's spelled __construct, not __contruct.

    Weedpacket thanks man ,its working but i have a problem again.

    invalid data source name
    what the hell it is ?

      It means your $dsn string is wrong (the data source name). Check it.

      Weedpacket $dsn = 'mysql;host=localhost;dbname=mvc';
      i try open my database with cmd its opening but my coe didn't working 🙁

        Weedpacket OMG hhaha its solve ,because i show you my dsn ,i know my mistake. just change ; to be : in mysql;host=localhost ,thanks man you very helpful

          "Excuse me, sir ... does your dog bite?" 😃

            Write a Reply...