IanC Posted December 30, 2012 Share Posted December 30, 2012 Hi Guys I am new here and a bit new to PHP. Having a bit of trouble... :S I am trying to modify a login script to save certain data from a mysql table as session data so I can easily recall it in the future... $sql="SELECT * FROM $tbl_name WHERE supplier_username='$myusername' and supplier_password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "index.php" session_start(); // store session data session_register("supplierloggedin"); $_SESSION['username']=supplier_username; $_SESSION['password']=supplier_password; $_SESSION['id']=supplier_id; $_SESSION['category']=supplier_category; $_SESSION['status']=supplier_status; $_SESSION['company_name']=supplier_company_name; $_SESSION['addressline1']=supplier_addressline1; $_SESSION['addressline2']=supplier_addressline2; $_SESSION['town']=supplier_town; $_SESSION['county']=supplier_county; $_SESSION['postcode']=supplier_postcode; $_SESSION['telephone']=supplier_telephone; $_SESSION['email']=supplier_email; Basically when I am recalling the data (example the 'username' session data) it comes back as the row name and not the data. For example I call the session data: <?php //retrieve session data echo "Pageviews=". $_SESSION['username']; ?> it prints "Pageviews=supplier_username I need it to print the username from the db. Cheers for your help if you can :S. Quote Link to comment https://forums.phpfreaks.com/topic/272509-setting-a-session-variable-as-a-cell-from-mysql-table/ Share on other sites More sharing options...
Jessica Posted December 30, 2012 Share Posted December 30, 2012 First turn on error reporting. Second read the PHP manual on MySQL. Specifically mysql_fetch_assoc Quote Link to comment https://forums.phpfreaks.com/topic/272509-setting-a-session-variable-as-a-cell-from-mysql-table/#findComment-1402115 Share on other sites More sharing options...
IanC Posted December 30, 2012 Author Share Posted December 30, 2012 Hi Jessica, Well there are no errors as far as I can see. I think I need to do something like this: $_SESSION['username']=[$row=supplier_username]; Am I on the right lines? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/272509-setting-a-session-variable-as-a-cell-from-mysql-table/#findComment-1402117 Share on other sites More sharing options...
NomadicJosh Posted December 30, 2012 Share Posted December 30, 2012 Yes, @IanC. You are on the right track. Quote Link to comment https://forums.phpfreaks.com/topic/272509-setting-a-session-variable-as-a-cell-from-mysql-table/#findComment-1402118 Share on other sites More sharing options...
IanC Posted December 30, 2012 Author Share Posted December 30, 2012 Many Thanks, With a bit of research I found what I needed to do. I opted out of storing all the data as a variable as it might become redundant or i might need to add more in the future. So instead I just stored the row id for the username so I can recall this in the future and pull the info directly from the row . I did this in the following way. Would love to know if there is a better way than an array though: $sql="SELECT * FROM $tbl_name WHERE supplier_username='$myusername' and supplier_password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "index.php" //make row_id a variable so it can be used in session $query="SELECT * FROM suppliers WHERE supplier_username='$myusername' and supplier_password='$mypassword'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $row_id=$row['supplier_id']; } //start the session session_start(); // store session data session_register("supplierloggedin"); //store the session row id $_SESSION['row_id']=$row_id; header("location:index.php"); } else { include "wronglogin.php"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272509-setting-a-session-variable-as-a-cell-from-mysql-table/#findComment-1402122 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.