Jump to content

[SOLVED] Sessions, MySQL, and PHP help


adamdeltoro

Recommended Posts

I've already created a table "users" and another called "facts", and a login system for my website. On one page I would like to display customized data depending upon whichever member/user is logged in. Depending upon which member is logged in, there are certain values (like 1's and 0's) in their respective rows for columns called (e.g.) school_a, school_b, etc. , that I would like to have correspond to different rows in my "facts" tables that would call those rows based upon the 1's and 0's in the user's rows.

How do you this? Would I need to have a session thingy going for my user, and somehow use that to then do a MySQL query on that user's data, then using the results, retrieve and print those rows under my "facts" table? Maybe some help with coding?

 

 

Link to comment
https://forums.phpfreaks.com/topic/146713-solved-sessions-mysql-and-php-help/
Share on other sites

A combination of sessions and queries is the way to go.

 

When logged in start a session and create session variables. I setup userID, and whatever other variables will need to be read frequently.

 

Then on a page for your facts check the session variable and pull information from that.

so when you set a session ID, you can set info that already exists from their corresponding "user" rows? If so, how would you do that? And how do you call data from the session id I set? I really appreciate your help, and could you perhaps give me an example in coding? I'm a wee-bit new to this.

If the user successfully logs in then put something like -

 

$_SESSION['id'] = $id;

 

Of course the $id variable above should already be set based upon a previous SQL statement where you did something like

 

$sql = 'SELECT id FROM users WHERE username = $_POST['username'] AND pass = $_POST['pass']';
$row = mysql_fetch_assoc(mysql_query($sql));
$id = $row['id'];

 

Be sure to put session_start(); at the beginning of every page to assure you can write and read to the session variable.  You call it at any time from any page (that has session_start();) by just using $_SESSION['id'].

 

This is very basic framework to get you started.  There's a little more if you want it secure and some error control.

 

 

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.