douna2014 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()
    {
        try
        {
            $this -> pdo = new PDO('mysql:host=127.0.0.1;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');
        exit();
    }
}

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 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 ......");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab