James
James

Reputation: 16339

Trouble storing user details in to $_SESSION

I've been wracking my brain for days trying to store user details fetched from a database query in to a $_SESSION variable.

I put it on the backburner whilst I finished building another part of the site and used dummy data that I typed in to test it.

The code works fine with the dummy data, but doesn't run when I try to implement the results of the query in to it. I've tried following how other people set this up, but mine just doesn't work.

Here is the script I have with dummy data that works:

$query = "SELECT * FROM members WHERE Email='$Email' AND Activation IS NULL";
$result = $dbc->query($query);
$password_match = 0;

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $password_match = password_verify($Password, $row["Password"]);
        }
        if ($password_match) {
            $_SESSION["Username"] = "Members username"; 
            header("Location: page.php");
        }
        else {
            $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
        }
}

The dummy data here is "Members username", I would love for it to store the value retrieved as part of the query from the column "Username".

I've tried a number of ways, most recently was using this:

$_SESSION["Username"] = $row["Username"];

But had no luck with this.

Any pointers much appreciated!

Upvotes: 0

Views: 37

Answers (1)

user1844933
user1844933

Reputation: 3417

Try this... You are not fetching row from inside loop,

$query = "SELECT * FROM members WHERE Email='$Email' AND Activation IS NULL";
$result = $dbc->query($query);
$password_match = 0;

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $password_match = password_verify($Password, $row["Password"]);

        if ($password_match) {
            $_SESSION["Username"] = "Members username"; 
            header("Location: page.php");
        }
        else {
            $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
        }
}}

Upvotes: 1

Related Questions