且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP检索当前登录用户的数据

更新时间:2023-12-04 07:58:22

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

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>