Assign table value as a session??


#1

When someone logs into my application - a login function runs from a pinc file. What is suppose to happen is if the username and password match - it is suppose to assign a session to the username - and to the first and last name which are in the table for the user. Can I assign a session variable to value in a mysql table? It does properly set the cookie I am assigning on the setCookie and does set the session username line but is is not setting the other two sessions. When the welcome page opens it is suppose to welcome them by first and last name and the username. It only displays the username.

Here is the function I am using:
[php]
session_start();
function getLogin($username, $pass)
{
$username = trim($_POST[‘email_address’]);
$pass = trim($_POST[‘password’]);

$db=mysql_connect(‘monet.homeip.net’,‘conn1’,‘conn1’) or die (“Cannot connect to server”);
mysql_select_db(‘test’,$db) or die (“Cannot select DB”);

// Mysql_num_row is counting table row
$result=mysql_query(“SELECT * FROM seebergerLogin WHERE username=’$username’ and password=’$pass’”);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched username and password, table row must be 1 row
if($count==1){
// Register $username, $pass
$row = mysql_fetch_array($result) or die(mysql_error());

$_SESSION[‘username’] = $username;
$_SESSION[‘fName’] = $first_name;
$_SESSION[‘lName’] = $last_name;
setcookie (“ok”, $username);
return TRUE;
}
else
{
echo “Wrong Username or Password”;
}
}
?>
[/php]
Here is the welcome page that should be displaying the information stored in the three sessions:
[php]<?
require(‘library.pinc’);
$cookie= $HTTP_COOKIE_VARS[“ok”];
if (!isset($cookie)){
header(“Cache-Control: no-cache, must-revalidate”);
}
echo "Welcome, ";
echo $_SESSION[ ‘first_name’] . " " . $_SESSION[‘last_name’], "
";

echo "You have successfully logged in as ";
echo $_SESSION[‘username’], "
";

echo "Your first and last names were passed by session data; your username was stored in the cookie. ", "
";
echo "The cookie value is: ";
echo $cookie, "
";
?>[/php]
I am thinking I am not fetching correctly - any suggestions?? Thanks!

[size=99px]**MOD EDIT - ADDED PHP TAGS FOR BETTER READABLITY -

Please review http://www.phphelp.com/forums/viewtopic.php?t=2752 on code submissions.[/size]


#2

What you’re missing is that mysql_fetch_array() returns an array :wink: Just like the name suggests. If you want to use data from the MySQL database, you’re gonna have to access it through the array.


#3

Thanks for the help. I did finally get it to work.


#4

Would you mind posting the solution?


#5

Sure -

This line:


$_SESSION['fName'] = $first_name; 
$_SESSION['lName'] = $last_name; 

changed to

$_SESSION['fName'] = $row['first_name']; $_SESSION['lName'] = $row['last_name'];

That’s all I needed to make it work. I am not sure this is the most efficient way to fix the problem but it worked.

Thanks again for your replies and help!