OOP返回有些无法访问的数组

时间:2014-04-09 01:18:50

标签: php arrays oop pdo

好的,所以我试图在类文件中返回一个数组,并像array ['variable']一样回显数组;但由于某种原因,它说[]括号内的变量是未定义的,但是当我对数组进行var_dump时,它会正常显示所有数组内容。这是我的代码:

Login.class.php

<?php

    class login {

        public $database;
        public $username;
        public $password;

        function __construct($database, $username, $password) {
            $this->database = $database;
            $this->username = $username;
            $this->password = $password;
        }

        public function encrypt() {
            return md5(md5($this->password));
        }

        public function doLogin() {
                $IsVerified = $this->database->getRows("SELECT * FROM `users` WHERE `username` = ? AND `password` = ?", array($this->username, $this->encrypt()));
                return $IsVerified;
        }
    }

这是login.php

if(isset($_POST['username']) && isset($_POST['password'])) {
    include('functions/config.php');
    include('functions/login.class.php');

    $database = new config(array());

    $login_func = new login($database, $_POST['username'], $_POST['password']);

    $UserData = $login_func->doLogin();

    echo $UserData['firstname'];
    //echo($UserData["email"]);
}

的config.php

<?php

    class config
    {
        public $isConnected;
        protected $datab;
        public $username;
        public $password;
        public $dbname;
        public $host;

        public function __construct($options=array()){
            $this->username = "root";
            $this->password = "";
            $this->dbname = "";
            $this->host = "localhost";
            $this->isConnected = true;

            try { 
                $this->datab = new PDO("mysql:host={$this->host};dbname={$this->dbname};charset=utf8", $this->username, $this->password, $options); 
                $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            } 
            catch(PDOException $e) { 
                $this->isConnected = false;
                throw new Exception($e->getMessage());
            }
        }

        public function Disconnect(){
            $this->datab = null;
            $this->isConnected = false;
        }

        public function getRow($query, $params=array()){
            try{ 
                $stmt = $this->datab->prepare($query); 
                $stmt->execute($params);
                return $stmt->fetch();  
                }catch(PDOException $e){
                throw new Exception($e->getMessage());
            }
        }

        public function getRows($query, $params=array()){
            try{ 
                $stmt = $this->datab->prepare($query); 
                $stmt->execute($params);
                return $stmt->fetchAll();       
                }catch(PDOException $e){
                throw new Exception($e->getMessage());
            }       
        }

        public function getRowCount($query, $params=array()){
            try{ 
                $stmt = $this->datab->prepare($query); 
                $stmt->execute($params);
                return $stmt->rowCount();  
                }catch(PDOException $e){
                throw new Exception($e->getMessage());
            }
        }

        public function insertRow($query, $params){
            try{ 
                $stmt = $this->datab->prepare($query); 
                $stmt->execute($params);
                }catch(PDOException $e){
                throw new Exception($e->getMessage());
            }           
        }

        public function updateRow($query, $params){
            return $this->insertRow($query, $params);
        }

        public function deleteRow($query, $params){
            return $this->insertRow($query, $params);
        }
    }

?>

无效的部分位于login.php&gt;&gt;&gt;

$database = new config(array());

$login_func = new login($database, $_POST['username'], $_POST['password']);

$UserData = $login_func->doLogin();

echo $UserData['firstname'];

当我尝试回显$ userdata ['firstname'];它说 “注意:未定义的索引:第13行的C:\ xampp \ htdocs \ site \ login.php中的名字”

但是当我使用var_dump时,它会显示所有已排列的内容。 非常感谢任何帮助。

这是所要求的var_dump

array(1) {
    [0]=> array(10) {
        ["id"]=> string(1) "0" 
        ["username"]=> string(7) "patrick" 
        ["password"]=> string(32) "432g1hjkgj2hgjhg34342" 
        ["groupid"]=> string(1) "1" 
        ["email"]=> string(15) "email@gmail.com" 
        ["verified"]=> string(1) "0" 
        ["birthday"]=> string(0) "" 
        ["firstname"]=> string(4) "john" 
        ["lastname"]=> string(9) "cena" 
        ["regdate"]=> string(0) "" 
    } 
} 

2 个答案:

答案 0 :(得分:2)

var_dump()可以让您更轻松,但在您的doLogin()方法中,您调用的是getRows()方法,而该方法又会在PDO对象上调用fetchAll()

所以你可能需要:

$UserData[0]['firstname']

修改:刚刚var_dump()确认...

答案 1 :(得分:0)

那么,它应该是$ UserData [0] ['firstname']。见PHP Doc on PDO::fetchAll