How to fix "Call to Undefined method Dbconnection::prepare() in Userdao.php on line xx" error?

Asked

Viewed 188 times

-1

  Meu projeto tem a seguinte estrutura:
  project
      \inc
          \DAO
              UserDAO.php
          \Entity
              User.php
          \Config
              AppConf.php
          DBConnection.php
      form.php
      login.php

And the following files:

Dbconnection.php

    class DBConnection {

        // Hold the class instance.
        private static $instance = null;
        public static $conn;
        private $host = AppConf::DBHOST;
        private $user = AppConf::DBUSER;
        private $pass = AppConf::DBPASSWORD;
        private $name = AppConf::DBNAME;

        // The db connection is established in the private constructor.
        private function __construct() {

            $this->conn = new PDO("mysql:host={$this->host};dbname={$this->name}", $this->user, $this->pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
        }

        public static function getInstance() {

            if (!self::$instance) {
                self::$instance = new DBConnection();
            }

            return self::$instance;
        }

Userdao.php

    require_once __DIR__ . '/../Entity/User.php';
    require_once  __DIR__. '/../config/AppConf.php';  
    require_once  __DIR__. '/../DBConnection.php';  

    class UserDAO {

        private $conn;

        public function __construct(DBConnection $conn) {

            $this->conn = $conn;

        }

        public function getUserByUsernamePassword($nm_username, $nm_password) {

            $sql  = "
            SELECT
            u.id_user,
            u.nm_username,
            u.nm_password,
            u.id_user_role,
            u.nm_email,
            u.id_status
            FROM
            user u
            WHERE 
            u.nm_username = :nm_username AND
            u.nm_password = :nm_password";
            $stmt = $this->conn->prepare($sql); 
            $stmt->bindParam(':nm_email',               $nm_username, PDO::PARAM_STR);
            $stmt->bindParam(':nm_password',            $nm_password, PDO::PARAM_STR);
            $stmt->execute(); 

            $user_obj = User::getNewInstance();

            try {

                $rs = $stmt->fetch(PDO::FETCH_ASSOC);   
                $id_user = $rs['id_user'];  

                echo 'id='.$id_user.'<br>';

            } catch (exception $e) {

                var_dump($e);
            }   

            // ...
            // ...
        }
    }

Appconf.php

    class Appconf {

        const DBHOST        = 'localhost';
        const DBNAME        = // DBNAME
        const DBUSER        = 'root';
        const DBPASSWORD    =  // password
        const DBDRIVER      = 'mysql';

    }

php form.

With theses Fields that are sent via POST 'login_submitted', 'nm_username', 'nm_password' to the login.php page

login.php

    require_once            'vendor/autoload.php';
    require_once  __DIR__ . '/inc/Util.php';
    require_once  __DIR__ . '/inc/Entity/User.php';
    require_once  __DIR__ . '/inc/DAO/UserDAO.php';
    require_once  __DIR__.  '/inc/config/AppConf.php';  
    require_once  __DIR__.  '/inc/DBConnection.php';  

    // Global Vars
    $gsErrMsg      = '';
    $gsName        = '';
    $gbErrLogin    = false;
    $gbErrPassword = false;

    processForm();

    // ==============================

    function processForm() {

        global $gsErrMsg, $gbErrLogin, $gbErrPassword;    

        $objConn = DBConnection::getInstance();
        $objUsr  = User::getInstance();

        // wheter Form submitted:
        if (isset($_POST['login_submitted']) && isset($_POST['nm_username'])) {

            // user and password required:
            if(!$_POST['nm_username'] || !$_POST['nm_password']) {

                $gsErrMsg = 'Please, fill correctly the login and password fields';
                $gbErrLogin = true;
                $gbErrPassword = true;

            } else {

                $sLogin     = trim($_POST['nm_username']);
                $sPassword  = $_POST['nm_password'];

                // Check if login and password are correct
                $objDAO = new UserDao($objConn);
                $objUsr = $objDAO->getUserByUsernamePassword($sLogin, $sPassword);
                unset($objDAO);

    ....
    ....

Giving me this error:

"Call to Undefined method Dbconnection::prepare() in Userdao.php on line 34"

    34 $stmt = $this->conn->prepare($sql);
  • Creating the method prepare in class DBConnection

  • the prepare method belongs to the PDO class, because it gives the error?

  • And where is the PDO class object? If the method is his, you must access it from it, it is not what you are doing now

  • i receive an instance of the PDO class object in Dbconnection. Then it would be normal to have the prepare method that is part of it. You can take a slow look at the code?

  • Your mistake lies in thinking that $userDAOInstance->conn is equal to dBConnectionInstance->conn, you are using composition and not inheritance

1 answer

0

change your line:

 $stmt = $this->conn->prepare($sql);

for:

 $conexao = $this->conn;
 $stmt = $conexao->prepare($sql);
  • This gives the message: "Call to a Member Function prepare() on null in /var/www/html/archs/admin/inc/DAO/Userdao.php:39" in the same line

Browser other questions tagged

You are not signed in. Login or sign up in order to post.