Sahidul Islam
Sahidul Islam

Reputation: 63

Call to undefined method in php

Here is my config.php

  <?php

    define('DB_HOST', 'localhost');
    define('DB_NAME', 'xxxx');
    define('DB_USER', 'xxxx');
    define('DB_PASS', 'xxxx');
    ?>

And It is DB.php

    <?php 
include 'config.php';


class DB {
    public static $pdo;

    public static function connection(){

        if (!isset(self::$pdo)) {

            try {

            self::$pdo = new PDO('mysql:host='.DB_HOST.'; dbname ='.DB_NAME,DB_USER, DB_PASS);
            }catch(PDOException $e){
                echo $e->getMessage();
            }

        }
        return self::$pdo;
    }


    public static function prepareOwn($sql){

        return self::connection()->prepare($sql);
    }
}



 ?>

3rd file is Student.php

<?php 
    include 'DB.php';



    class Student {
        public $table = 'student_info';

        public function readAll(){
            $sql = "SELECT * FROM $this->table";

            $stmt = DB::prepareOwn($sql);
            $stmt->execute();
            return $stmt->fetchAll();
        }   
    }
 ?>

But When I try to access readAll() from index.php using spl_autoload_register() Then I can see Fatal error: Call to undefined method DB::prepareOwn()

Can anyone help me to solve the problem??

Many thanks. Sahidul

Upvotes: 1

Views: 2158

Answers (2)

mohammed
mohammed

Reputation: 26

i hope this code will work for you

class MySQLDatabase {

    // Class attributes
    private $host_name = "localhost";
    private $database_name = "XXXXXXX";
    private $database_username = "XXXXXXX";
    private $database_password = "XXXXXXX";
    private $is_connected;
    private $connection;

    private $statement ;


    // construct
    public function __construct() {

        $this->open_connection();

    }// End of construct

    // connection method
    public function open_connection() {

        try {
            $this->is_connected = TRUE ;
            // PDO Connection
            $this->connection = new PDO("mysql:host=".$this->host_name.";dbname=".$this->database_name.";charset=utf8",$this->database_username,$this->database_password);
            // Error reporting
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES,FALSE);

        } catch(PDOException $errors) {

          $this->is_connected = FALSE ;

          self::catch_errors($errors);
        }



    }// End of open connection method

    // Get connection method
    public function connection(){
        return $this->connection ;
    }

    // Close connection method
    public function close_connection() {

        $this->connection = null;

    }// End of close connection method

    private static function catch_errors($errors) {
        echo("<h4><p>" . $errors -> getMessage() . "</p></h4>");
        die();
    }


    // query method

    public function query($sql){

    return $this->statement = $this->connection->prepare($sql);

    }




}// End of database class

$database = new MySQLDatabase();





    class Student {

        protected static $table = 'My_table';

        public function readAll(){
            global $database;       
            try{

                $sql = "SELECT * FROM ". self::$table;
                $stmt = $database->query($sql);
                $stmt->execute();
                return $stmt;

            }catch(PDOException $error){

                echo("<h4><p>" . $errors -> getMessage() . "</p></h4>");
                die();

            }

        }   
    }
    $c = new Student();     
    $s = $c->readAll();
    $stmt = $s->fetchAll(PDO::FETCH_ASSOC); 

    foreach($s as $v){

        var_dump($v);

    }

Upvotes: 0

Mostafa Ghanbari
Mostafa Ghanbari

Reputation: 374

i copied your code into mine and saw your error. but as i guessed, first you will get an error with this line inside db.php:

return self::$pdo->prepare($sql);

Fatal error: Call to a member function prepare() on null

where prepare function came from? $pdo is just a static property in this class and it doesn't have a function called prepare! fix this line

Updated
the problem is you forgot to call connection method inside your prepareOwn. so your new prepareOwn function should be:

public static function prepareOwn($sql) {
    self::connection();
    return self::$pdo->prepare($sql);
}

Upvotes: 1

Related Questions