Jump to content

Recommended Posts

Hello everyone.

I need to set some sessions. I have the values stored in a Mysql table.

How can i set the session values to the ones in the Mysql table?

[code]<?php
$host="********"; // Host name
$username="******"; // Mysql username
$password="******"; // Mysql password
$db_name="*********"; // Database name
$tbl_name="web_members"; // Table name

// 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 signup form
$myemail=$_POST['myemail'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE Email='$myemail' and Password='$mypassword'";
$result=mysql_query($sql);

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

if($count==1){
// Register $myusername, $myemail and redirect to file "login_success.php"
$_SESSION['myemail'] = $myemail;
$_SESSION['mypassword'] = $mypassword;

header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

[/code]

Would it be something like:

[code]$_SESSION['fname'] = Select First Name from web_members where Email='$myemail' and Password='$mypassword'  [/code]

I really need to know how to carry variables in sessions. Once i drag the values from the Mysql table into session variables then i can echo them out.

I just need this step. Please please pleaseeeee!!!!!!!! :)

I've looked everywhere!

Do i need mysql_fetch_assoc

??? If so how would i use it exactly. As these bloody blank pages are getting on my nerves.

Snooble
Link to comment
https://forums.phpfreaks.com/topic/36347-session-setting/
Share on other sites

exampe:

page1.php
[code]
<?php
  // start your session
  session_start();

  // code to connect to db here

  // query string. just an example to pull all info from 'something'
  $sql = "select something from table";
  // execute the query and put the result SOURCE in $result
  $result = mysql_query($sql);

  // loop through the result source to pull out the rows 1 at a time
  // until there are no more rows to pull out
  while ($list = mysql_fetch_assoc($result)) {
    // assign each row to a session variable. we're going to make it an array
    $_SESSION['blah'][] = $list['something'];
  } // end while

  // now lets redirect to another page for example of session var usage
  header("Location: page2.php"); exit();
?>
[/code]

page2.php
[code]
<?php
  //always must use this to tell php you have a session going on
  session_start();

  // if it exists...
  if ($_SESSION['blah']) {
      // example of echoing out each 'something'
      echo $_SESSION['blah'][0]; // echo out first one
      echo $_SESSION['blah'][2]; // echo out third one
      echo $_SESSION['blah'][6]; // echo out seventh one

      // example of looping through all of them...
      foreach($_SESSION['blah'] as $key => $val) {
        echo "element: $key  value: $val <br/>";
      } // end foreach
  // end if exists
  // else, if it doesn't exist...
  } else {
      // do something like tell user it doesn't exist, go back to some page with
      // a header call, or whatever you wish to do for error handling
  } // end else
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/36347-session-setting/#findComment-172834
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.