Jump to content

Storing MySql data in a session


seanj43

Recommended Posts

Here is my code...

 

<?php
mysql_connect("localhost", "user", "pass")or die("cannot connect"); 
mysql_select_db("database")or die("cannot select DB");

$myemail = mysql_real_escape_string($_POST['myemail']);  
$mypassword = mysql_real_escape_string($_POST['mypassword']); 
$mypassword = md5($mypassword);

$myemail = stripslashes($myemail);
$mypassword = stripslashes($mypassword);

$sql="SELECT * FROM users WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_start();  
$_SESSION["myemail"]= "$myemail"; 
header("location:home.php");
}
else {
header("location:fail.php");
}
?>

 

What can I do to this code so that it will also store first name from the database inside a session?

 

Link to comment
https://forums.phpfreaks.com/topic/217116-storing-mysql-data-in-a-session/
Share on other sites

Move session_start to the first line after <?php

 

$sql = "SELECT * FROM users WHERE email='$myemail' and password='$mypassword'";

$result = mysql_query($sql) or die ("Error in query: $sql. " . mysql_error());

 

//check rows

if (mysql_num_rows($result) == 1) {

$row = mysql_fetch_assoc($result);

 

$_SESSION['ufname'] = $row ['ufname'];  //this is your first name column name

$_SESSION["myemail"]= "$myemail"; header("location:home.php");

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.