PHP检索当前登录用户的数据 [英] PHP retrieving data of user currently logged in

查看:79
本文介绍了PHP检索当前登录用户的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法显示当前登录用户的信息.我不确定这里做错了什么.我的登录页面和注销功能正常运行,当我注册用户时,它将该用户添加到数据库中,但是我希望能够在他们登录时显示当前用户的数据.

I can't seem to display the information of the user currently logged in. I'm not sure what I'm doing wrong here. I've got my login page and logout function working correctly, and when I register a user it add that user to the database, but I want to be able to display the current users data when they log in.

<?php 
session_start();

// connects to the database
$mysqli = new mysqli("localhost", "root", "");


$query = "SELECT firstName, lastName, username, password, loyaltyPoints FROM user WHERE username = '".$_SESSION['username']."'";

if($result = $mysqli->query($query))
{
while($row = $result->fetch_assoc())
{

    echo $row['firstName'];
    echo $row['lastName'];
    echo $row['username'];
    echo $row['password'];
    echo $row['loyaltyPoints'];

}
$result->free();
}
else
{
echo "No results found";
}
?>

<html>

<head>  
</head>
<body>
Welcome! Click here to <a href="logout.php" tite="Logout">Logout.</a>
    or here to go to the home page --> <a href ="INDEX.html">HOME</a>
</body>
</html>

推荐答案

首先,您尚未选择任何数据库.先这样做.

First off, you haven't selected any database yet. Do that first.

您当前的代码:

$mysqli = new mysqli("localhost", "root", ""); // missing fourth parameter

在此添加.

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();

// connects to the database
$mysqli = new mysqli('localhost', 'root', '', 'DATABASE NAME'); // add the database name (fourth parameter)

if(empty($_SESSION['username'])) {
    echo 'not logged in';
    exit;
}

$query = "SELECT firstName, lastName, username, password, loyaltyPoints FROM user WHERE username = '{$_SESSION['username']}'";
$result = $mysqli->query($query) or die($mysqli->error);

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {

    //  echo $row['firstName'];
    //  echo $row['lastName'];
    //  echo $row['username'];
    //  echo $row['password'];
    //  echo $row['loyaltyPoints'];

        foreach($row as $val) {
            echo "$val <br/>";
        }
    }   
} 

else {
    echo 'not found';
}

?>

<html>

<head>  
</head>
<body>
Welcome! Click here to <a href="logout.php" tite="Logout">Logout.</a>
    or here to go to the home page --> <a href ="INDEX.html">HOME</a>
</body>
</html>

这篇关于PHP检索当前登录用户的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆