Snooble Posted January 30, 2007 Share Posted January 30, 2007 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 rowif($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 Quote Link to comment https://forums.phpfreaks.com/topic/36347-session-setting/ Share on other sites More sharing options...
ninja Posted January 30, 2007 Share Posted January 30, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/36347-session-setting/#findComment-172834 Share on other sites More sharing options...
Snooble Posted January 30, 2007 Author Share Posted January 30, 2007 thank you i will test it this evening.Snooble (Looks all good to me) Quote Link to comment https://forums.phpfreaks.com/topic/36347-session-setting/#findComment-172847 Share on other sites More sharing options...
Snooble Posted January 31, 2007 Author Share Posted January 31, 2007 [code] $_SESSION['blah'][] = $list['something'];[/code]what do i put in "blah" and "something"'s places?thanksSnooble Quote Link to comment https://forums.phpfreaks.com/topic/36347-session-setting/#findComment-173623 Share on other sites More sharing options...
Snooble Posted January 31, 2007 Author Share Posted January 31, 2007 At the moment i recieve the output:[code]element: 0 value: [/code]I assume it is to do with what i pointed out?Snooble Quote Link to comment https://forums.phpfreaks.com/topic/36347-session-setting/#findComment-173637 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.