Fatal error: Call to undefined method Database::prepare()

拟墨画扇 提交于 2019-11-29 07:01:21

You create $dbh when you instantiate Database(), but instantiating the Database only return an instance of your Database class, not your db connection. You should have a getDb to get your connection from database object:

$dbClass = new Database();
$dbh = $dbClass->getDb(); // here you get the connection
$users= new Users($dbh);  // here you give to Users() the $dbh, that isn't your 
                          // connection.. it's just Database class

Database construct only return an instance of your Database class, not your db connection

class Database{

 private $db;


public function __construct(){

    try {
     $this->db = new PDO("mysql:host=$hostname;dbname=kamadhenu_web", $username, $password);
    /*** echo a message saying we have connected ***/

   }
    catch(PDOException $e)
        {
            echo $e->getMessage();
       }    
 }

 public function getDb() {
       if ($this->db instanceof PDO) {
            return $this->db;
       }
 }


}

add the method "getmyDB" to database file

class Database

    {
    /* Properties */
    private $conn;
    private $dsn = 'mysql:dbname=test;host=127.0.0.1';
    private $user = 'root';
    private $password = '';
    /* Creates database connection */
    public

    function __construct()
        {
        try
            {
            $this->conn = new PDO($this->dsn, $this->user, $this->password);
            }

        catch(PDOException $e)
            {
            print "Error!: " . $e->getMessage() . "";
            die();
            }

        return $this->conn;
        }

    public function getmyDB()
        {
        if ($this->conn instanceof PDO)
            {
            return $this->conn;
            }
        }
    }

and call it when you create the constructor in the file user.php

include "database.php";

class User

    {
    /* Properties */
    private $conn;
    /* Get database access */
    public

    function __construct()
        {
        $this->conn = new Database();
        $this->conn = $this->conn->getmyDB();
        }

    /* Login a user */
    public

    function login()
        {
        $stmt = $this->conn->prepare("SELECT username, usermail FROM user");
        if ($stmt->execute())
            {
            while ($rows = $stmt->fetch())
                {
                $fetch[] = $rows;
                }

            return $fetch;
            }
          else
            {
            return false;
            }
        }
    }

and finally add test.php file

include "user.php";

$user = new User();
$list = $user->login();

 foreach($list as $test)
    {
    echo $test["username"];
    }

Your Database class does not extend PDO neither it does implement prepare method.

In order to access your PDO object, you must make it public and access like:

From User class:

$this->db->db->prepare();

The best way would be to extend the PDO class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!