Jump to content

Setting A Session Variable As A Cell From Mysql Table


IanC

Recommended Posts

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.

Link to comment
Share on other sites

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";

}

?>

Link to comment
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.