Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Session Variable

session session variable

  • Please log in to reply
8 replies to this topic

#1 thumbliner

thumbliner

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 09 May 2011 - 07:22 AM

I have a login page and a restricted access page. Both coded by DW, working perfectly fine.

There is SESSION variable declared on the login page and when I try to echo it on the other (admin_lounge.php) page, it is working perfectly.

---
Problem

1. I want to fetch the firstname from the database based on who is logging in.
2. Then echo it on the next page.
3. Not working. :P

The code I am trying to use is as follows -

$salman = $_SESSION['MM_Username'];

mysql_select_db($database_brn_system, $brn_system);
$query_rsPilot = "SELECT pilots.firstname, FROM pilots WHERE pilots.pilot_callsign = '$salman'";
$rsPilot = mysql_query($query_rsPilot, $brn_system) or die(mysql_error());
$row_rsPilot = mysql_fetch_assoc($rsPilot);

session_register('firstname');

$_SESSION['firstname'] = $row_rspilot['firstname'];

---

Please help.
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 09 May 2011 - 08:53 AM

Your query seems ill formed, you can remove all of the pilot. prefixes as you are not working with another table (especially as you have the FROM statement pointing to the same table). Also there is an erroneous comma after your column selection.

Other than that, session_register has been replaced for over ten years (It astounds me why I see it today), you may wish to remove that to keep the next call clean.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 thumbliner

thumbliner

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 09 May 2011 - 09:11 AM

Alright. There you go.

$LoginRS__query=sprintf("SELECT pilot_callsign, password, staff_level, firstname FROM pilots WHERE activated = 1 AND pilot_callsign=%s AND password=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $brn_system) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'staff_level');
    
	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
	
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;

Can you just give me the one line code for declaring a SESSION VARIABLE for the 'firstname' column?
Thanks
  • 0

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 09 May 2011 - 09:23 AM

You had it right the first time with $_SESSION['firstname'] = $row_rspilot['firstname'];

Your session_register was invalid in your context, as it takes a variable name ($firstname which did not exist).

Your code seems a little outdated as well, I would certainly consider using mysqli instead of (sprintf and getsqlvaluestring), prepared statements are at least a hundred times safer than those. Your code also checks for version 5.1 which is 6 years old and may be unnecessary.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 thumbliner

thumbliner

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 09 May 2011 - 10:24 AM

Mate, this is my query

$LoginRS__query=sprintf("SELECT pilot_callsign, password, staff_level, firstname FROM pilots WHERE activated = 1 AND pilot_callsign=%s AND password=%s",

How can I use $row_rspilot['firstname']?
Something tells me that it has to be something else. Just confirm.

There is no record set named rspilot.

What do I use in this case?

---

As for the code, talk to adobe. I am using CS5. :)
  • 0

#6 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 09 May 2011 - 10:33 AM

Your question seems a little vague, was the code in your first post paraphrased?

mysql_result will only fetch one result (in this case staff_level) so you cannot retrieve any other information from your SELECT query. You must replace it with mysql_fetch_assoc or related functions to get the first name, and only then can you use it.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#7 thumbliner

thumbliner

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 12 May 2011 - 03:39 AM

Naah, that fails too
  • 0

#8 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 12 May 2011 - 12:27 PM

Naah, that fails too

You do not seem to be assigning $_SESSION['firstname'] regardless if you have retrieved the row (with mysql_fetch_*)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#9 rySch

rySch

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 13 May 2011 - 12:20 PM

edit, I must learn to read more! :P

edit 2:
this would work just as well

mysql_select_db($database_brn_system, $brn_system); //select the database

$query = "SELECT `firstname` FROM `pilots` WHERE `pilot_callsign` = '{$_SESSION['MM_Username']}' LIMIT "1; //compile the query

$result = mysql_query($query_rsPilot, $brn_system) or die(mysql_error());//run the query
$row = mysql_fetch_assoc($rsPilot);//get any returned values

if($row['firstname'] != ''){ //if there was a name found
$_SESSION['firstname'] = $row['firstname'];
}else{
echo "No Name Found";
}

Ryan,
  • 0





Also tagged with one or more of these keywords: session, session variable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download