【发布时间】:2014-05-21 23:32:51
【问题描述】:
好的,所以我试图在类文件中返回一个数组,并像 array['variable']; 一样回显该数组;但由于某种原因,它说 [] 括号内的变量是未定义的,但是当我 var_dump 数组时,它会正常显示所有数组内容。这是我的代码:
登录.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中>>>
$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) ""
}
}
【问题讨论】:
-
它可能是一个对象,而不是一个数组。尝试访问像 $userdata->firstname 这样的属性
-
已经试过了,说不是对象“尝试获取非对象的属性”
-
可以加
var_dump()吗? -
下一次,您需要自己找到问题所在,就是在回显之前使用 var_export($UserData)。这样您就可以清楚地识别您正在尝试访问的内容。如果为空,请尝试返回并调试数据丢失的原因
-
所有这些工作都使用 MD5 (tsk tsk)。使用 CRYPT_BLOWFISH 或 PHP 5.5 的
password_hash()函数。