777string
777string

Reputation: 23

How can I get data from multiple mysql tables and store in user session?

I have two mysql tables: USER and ROSTER. Previously, I've been able to query and retrieve the proper student and direct the user to the proper page. Moving forward, I'd like to query the tables and retrieve the users studentID, name, period, and access.

USER
studentID
name
access

ROSTER
id
period

    $studentID = $_POST['studentID'];

    $studentID = stripslashes($studentID);

    $studentID = mysql_real_escape_string($studentID);
    $getUser_sql = "SELECT * FROM USER, ROSTER WHERE USER.studentID='$studentID'";
    $getUser = mysql_query($getUser_sql);

    $getUser_result = mysql_fetch_assoc($getUser);

    $getUser_RecordCount = mysql_num_rows($getUser);

    if($getUser_RecordCount==1){



       session_start();
       $_SESSION['loggedin'] = TRUE;
       $_SESSION['studentID'] = $studentID;


    header("location:login_success.php");

    }

What am I doing wrong? I've looked at examples @tizag and @mysql, but I just can't get this going.

Upvotes: 1

Views: 583

Answers (1)

Jakub
Jakub

Reputation: 20475

You are not doing a proper JOIN, so I am assuming the join is 1 to 1:

SELECT studentID, name, access, period 
FROM USER JOIN ROSTER ON user.studentid = roster.id 
WHERE USER.studentID = '$studentID'

The 'ON' is the important part as it defines WHAT to join ON, the WHERE in this SQL is a filter of the OVERALL data.

Upvotes: 1

Related Questions