2014-05-02 19:36 阅读 48

PHP - 无法从另一个类访问数据库连接器类。 我究竟做错了什么?

I'm trying to accomplish the same ends as outlined in this question but my application of that answer just does not work. I get blank white screens when attempting to perform any operation involving the database class.

This should be simple-- a user inputs a username and password into a form. If both are received by the controller, I query the database and compare a hash of the submitted password with the hash on file. The problem is, my page does not load once I start making database calls.

I have a controller and two classes. One class is a database connector, the other is an authentication module that depends on the database connector.

Database connector class:

class inquiry
    protected $pdo;

    // Set up the initial db connection
    function __construct()
            $this -> pdo = new PDO('mysql:host=;dbname=mysqlserver','mydatabase','ffffffffffffffff');
            $this -> pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this -> pdo -> setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
            $this -> pdo -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            $this -> pdo -> exec('set names "utf8"');
        catch (PDOException $e)
            echo $e -> getMessage();

Then, the authentication class:

class auth
private $username;
private $password;
private $db;

function __construct(inquiry $db)
            $this -> db = $db;

function login($username, $password)
    $this -> username = $username;
    $this -> password = $password;

    // Query database, get hashed password for username
    $this -> db -> query('select password from users where username="bob";');

    // Data needs to be fetched but PHP does not process anything past this point anyway

    return true;

Then, in the controller:

require_once '../inc/class.inquiry.php';
require_once '../inc/class.auth.php';

if (isset($_POST['username']) && isset($_POST['password']))
    // Probably doing something wrong here
    $dbconnect = new inquiry();
    $user = new auth($dbconnect);

    $authorized = $user -> login($_POST['username'], $_POST['password']);

    if ($authorized == true)
        // Send user to index page
        header('Location: index.php');

I've commented the sections where I think I'm going wrong, but I don't know what to actually do about it. Any tips would be appreciated!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanou3868 duanou3868 2014-05-02 19:47

    All of your code is wrong on many levels. Starting from code standards where class should start with uppercase letter, formatting the code, finishing to calling a non-existent method.

    First of all, your inquiry class does not have the desired query() method. It seems you try to give us a code you have not written by yourself, to debug.

    Second, your class is completely USELESS. Even though it does not have a custom wrapping query method, you still could use PDO's method for querying and execute a query. However, even you are assigning value of object of type PDO to your protected $pdo you have absolutely NO ACCESS to this $pdo outside the class i.e. from auth class. You should write an accessor for $pdo, so you can use something like

    $this->db->getPdo()->prepare("SELECT ......");
    点赞 评论 复制链接分享