Hi, new to the forum! I have a simple PHP question that I couldn’t seem to find a sufficient answer for by searching. I have a small MySQLi DB and want to retrieve something from “members”, based on which user is currently logged on. Something like “Hi member!” but replacing “member” with the matching logged in user’s name (member_name) from the DB. I know about “select” and all, but I just can’t figure it out. Any help would be most appreciated!
Generally, when a user logs in you would set a session variable containing the data you want to display in the app and then just echo that variable where you want it displayed.
This only applies to something you would want to persistently display such as the persons name. Other data would be queried as needed.
Where are these variables magically coming from? If your using a Database the values would come from the query result. If you set the session properly you would echo the session.
I would suggest you take a look at the countless scripts and tutorials that are out there and actually learn about what you are trying to do. It’s pretty clear you are just throwing stuff out hoping it sticks to the wall without understanding what you are doing.
Your OP says you want to " retrieve something from “members", based on which user is currently logged on." yet you keep posting code related to registering a user. You arent “retrieving” anything.
I need a simple way of echoing the logged in user’s name on a particular PHP page in my site. In my login routine I added this right after the user is authenticated (the username is stored in “member_name” in my DB:
$_SESSION["node"] = $user[0]["username"];
I also tried many variations such as:
$_SESSION["node"] = $member_name;
etc. Then in my main logged in page, I put this:
<?php echo ($_SESSION['node']);?>
It doesn’t work. lol Am I even close here? And is there an easier way? Thanks!
What should that mean? No one here sits in front of your computer. You can output variables at any point with var_dump(), right after the assignment and where you want to show something. There’s a null value or not what you expected? Just use the next upper key of the array or the complete array with var_dump().
We’d need to see the code in context to have a chance of trying to help figure out what’s wrong. There is nothing wrong with any of the lines of code you posted, in isolation, so the key will be how they interact with the rest of the code.
Is that right at the start of your PHP code? Before you call session_start()?
function getMemberByUsername($username) {
$db_handle = new DBController();
$query = "Select * from members where member_name = ?";
$result = $db_handle->runQuery($query, 's', array($username));
return $result;
}