0

I'm sure this has been asked a few times, but I can't seem to find a straight forward or well explained answer. I'm attempting to create a Account Object/Class within PHP, where I can retrieve data from an account. The issue I'm having is that I cannot include my database.php class so I can retrieve all the created accounts.

I'm fairly new with PHP and don't have an expansive knowledge on how a lot of the processes or conventions work. The error/warning I'm running into is this;

Warning: include(../database.php): failed to open stream: No such file or directory in ~/account.php on line 3

Here is my account.php class

<?php

include '../database.php';

class Account {

    protected $username, $email;

    protected function getEmail() {
        return $this->email;
    }

    protected function getUsername() {
        return $this->username;
    }

    public function __construct($username, $email) {
        $this->username = $username;
        $this->email = $email;
    }

    public static function getUser($username) {
        $statement = getConnection()->prepare('SELECT * FROM account WHERE username=?');
        $statement->bindValue(1, $username);

        if ($rows = $statement->fetch()) {
             $account = new Account($rows['username'], $rows['email']);
        }

         return $account;
    }

}

Here is my database.php incase I've done something wrong or may need to change things

<?php

function getConnection() {
    $dbh = new PDO("mysql:host=127.0.0.1;dbname=mcsl;port3306", "root", "");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    return $dbh;
}

Directory Structure:

Directory Structure

10
  • Directory May Not Be Correct. Commented May 30, 2016 at 15:09
  • Where is account.php * database.php pages saved. Commented May 30, 2016 at 15:10
  • According to your error, your path for your database.php file is incorrect. Commented May 30, 2016 at 15:10
  • @NanaPartykar I thought the same, but I'm using the same include function in another php file in the same directory, and it's working fine. Commented May 30, 2016 at 15:11
  • Can you please post your directory structure for account.php, database.php page. Commented May 30, 2016 at 15:12

2 Answers 2

3

You can use include (dirname(__FILE__)."/../database.php") to access the database.php file in the account.php file

Sign up to request clarification or add additional context in comments.

1 Comment

There is a troubleshooting checklist for this problem : stackoverflow.com/questions/36577020/…
0

You currently have a mix of object-oriented and procedural code.

Adding database features to a class is normally accomplished by dependency injection. In other words, you pass the database object as argument to whatever method needs it and use type hinting to enforce it, e.g.:

public static function getUser(MyConnectionClassOrInterface $db, $username) {
}

As about your include, you're using relative paths, which are relative to main script, not current file. You can build absolute paths or use a more elaborate class auto-loader.

1 Comment

There is a troubleshooting checklist for the include problem : stackoverflow.com/questions/36577020/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.