Sanjula Nipun
Sanjula Nipun

Reputation: 17

PHP and MySQL Select a SESSION user detals

i need to get the current session user email and phone number.

<p>Hello <?php echo htmlspecialchars($_SESSION["username"]); ?></p>
<?php
require_once "phpconfig/dbconfig.php";
$sql = "SELECT email FROM users WHERE username ='($_SESSION["username"])'";

if($result = $pdo->query($sql)){
echo "<p>" . $result['email'] . "</p>"; 
}
unset($pdo);
?>

Upvotes: 0

Views: 62

Answers (1)

Towsif
Towsif

Reputation: 320

To persist user data from one page to another page, you've to depend on session in php. You've to start session on every page by using session_start(). The PHP session_start() function checks for an existing session ID first. If it finds one, i.e. if the session is already started, it sets up the session variables and if doesn’t, it starts a new session by creating a new session ID.

Change this

$sql = "SELECT email FROM users WHERE username ='($_SESSION["username"])'";

Replace with

$sql = "SELECT email FROM users WHERE username = :username";
$stmt = $conn->prepare($sql);

$username = $_SESSION["username"];
$stmt->execute([':username'=> $username]);

To get the output, use the following code

var_dump($stmt->fetch(PDO::FETCH_ASSOC));

Upvotes: 1

Related Questions