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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器