1

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
    Do you have session_start() on both pages? Commented Apr 6, 2012 at 21:49

1 Answer 1

1

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'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks i was missng the $row = mysql_fetch_assoc($result);
what does the mysql fetch assoc do?
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.