Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7758133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:16:41+00:00 2026-06-01T13:16:41+00:00

I am trying to assign a value from a query result to a session

  • 0

I am trying to assign a value from a query result to a session but it isn’t working..

Session username
below, i am registering the session username which is the users email

$_SESSION['myusername'] = $myusername;

check login
Here i am trying to get the customer name from the same table were the email is stored and save it in the “name” session

$_SESSION['name'] = $row['name']; 

Logged success
here i am trying to fetch the user’s name

echo "Welcome ". $_SESSION['name'];

check login page*

<?php
ob_start();
$host="localhost"; // Host name 
$username="xx"; // Mysql username 
$password="xx"; // Mysql password 
$db_name="xx"; // Database name 
$tbl_name="customer"; // Table name
$db_usercol="email"; // Field containing username
$db_passcol="password"; // Field containing password

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE $db_usercol='$myusername' and $db_passcol='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "profile.php"
session_register("myusername");
session_register("mypassword");
$_SESSION['myusername'] = $myusername;
$_SESSION['name'] = $row['name']; 
header("location:profile.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

on successful login*

<?
// Check if session is not registered , redirect back to main page. 
// Put this code in first line of web page. 
session_start();
if(isset($_SESSION[$myusername])){
header("location:members.php");
}
else
include("header.php");
?>

<body id="profile">
<div id="wholewrap">
<div id="bodywrap">

<div id="content">
<h1>Your Profile</h1>

<?php

echo "Welcome ". $_SESSION['myusername'];
echo "Welcome ". $_SESSION['name'];
?>

</div>  <!-- content #end -->

<div id="sidebar">

Lorem Ipsum

</div>  <!-- sidebar #end -->

</div>  <!-- bodywrap #end -->
</div>  <!-- wholewrap #end -->

 <?php include("footer.php"); ?>

The result is

I only get the username but not the name of the user, which is a field in the same table named name

result

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:16:42+00:00Added an answer on June 1, 2026 at 1:16 pm

    First you have to have a query, something like this

    $query = mysql_query("SELECT `name` FROM `users` WHERE `email`='".$_POST['email']."'") or die(mysql_error()); // of course you want to clear the strings and validate them
    

    then you do

    if(mysql_num_rows($query) > 0)
    {
       $row = mysql_fetch_assoc($query);
    
       $_SESSION['name'] = $row['name'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to assign a variable from a html text input value and then
I'm trying to export certain fields from the query below to a Text file.
I am trying to assign a value from my database to a variable inside
I am trying to get a value fro query string and assign that value
I´m trying to upload a file which is fetched from a server. But when
Im trying to get a value from a text field (entered by the users)
I'm trying to assign char* values into a queue from a loop. I keep
I have been trying to assign value to an object array that is defined
I have an asp.net mvc application and i am trying to assign value to
I'm trying to assign a value to a series of variables inside a FOR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.