I'm doing a project where I'm supposed to create a website that people can register and log in. I'm trying to do that thing where it will say in the corner "Logged in as ..."
This is part of the code that assigns the variable name to the name value in the database when the user logs in.
$first = mysql_query("SELECT FirstName FROM students WHERE studentID = '$user' AND password = '$pass'");
$firstname = mysql_fetch_array($first);
$_SESSION['name'] = $firstname;
And this is the code on the website that displays the name
<?php
if(isset($_SESSION['name']))
{
echo '<li class="disabled"><a href="#" disabled>Signed in as ' . $_SESSION['name'] . '</a></li>';
}
?>
But when the website is actually ran the space is left blank. I know the variable is set because I replaced $_SESSION['name'] with some random string and it echo'd it fine. I also ran the SQL query and it gave me back the name. What am I missing from my code?
mysql_fetch_array()
, as the name implies, returns an array of values. Not just the one column you have selected.
$row = mysql_fetch_array($first);
$_SESSION['name'] = $row['FirstName'];
Try replacing
mysql_fetch_array($first);
with
mysql_fetch_array($first)['FirstName'];
Also avoid using mysql_* function. They have been depricated. Use mysqli_* instead or PDO
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.