【发布时间】:2016-05-04 08:05:50
【问题描述】:
我已经能够从 MySQL 数据库中查询数据并将数组放入变量 $results,但是我无法将数组的一部分放入 $results 变量中,例如密码或注册时间成单独的变量。我做错了什么?
这是我的数据库类中的选择函数,我使用 $query 的参数从数据库中查询数据。这适用于我的帖子页面,所以我认为这不是问题。
// Select Function
public function select($query){
$result = $this->connection->query($query);
while ( $obj = $result->fetch_object()){
$results[] = $obj;
}
return $results;
}
这是我的查询类中的登录函数,我将提交的用户名放入查询中,然后将其传递给上面的选择函数以使用该用户名提取结果,该用户名返回到登录函数并存储在$结果变量。然后它检查变量是否为空,如果是它会杀死脚本,这一切都在我得到的工作:
1)yay!!!!
2)print_r()显示的内容是:
Array ( [0] => stdClass Object ( [ID] => 1 [username] => dillon [fname] => Dillon [lname] => Erhardt [email] => dillon@erhardt.co.uk [password] => dilandsas [regtime] => 1453685794 ) )
function login($redirect) {
global $db;
if(!empty($_POST)) {
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
$query = "
SELECT * FROM users
WHERE username = '$username'
";
$results = $db->select($query);
//Kill the script if the submitted username doesn't exit
if (empty($results)) {
die('Sorry, that username does not exist!');
}
echo "yay";
print_r($results);
//The registration date of the stored matching user
$storeg = $results['regtime'];
//The hashed password of the stored matching user
$stopass = $results['password'];
问题是,当我尝试使用 $results['password']; 或 $results['regtime']; 将密码或 regtime 中的值存储在我的 $storeg 和 $stoppass 变量中时,我会收到这些通知
Notice: Undefined index: regtime in /Applications/MAMP/htdocs/Test/CMS-v2/includes/class-query.php on line 55
Notice: Undefined index: password in /Applications/MAMP/htdocs/Test/CMS-v2/includes/class-query.php on line 58
Invalid Login
如何将数组中的密码和注册时间值存储到 $results 变量中?
【问题讨论】:
-
请列出您的“用户”表字段吗?
标签: php html mysql arrays database