Link Mysql tables based on user session

I need your help making a script to get user data from two user login tables.

This is my config:

  • I have used THIS GUIDE using registration form and session

  • My files:

---- Login.php (This file contains the html login form)
---- db_connect.php (This is for connecting to the database)
---- process_login.php (This manages the connection data and redirects the user after login )
---- function.php (This is for sec_session_start ();

and a function to fetch data from the database)

Database tables:

  • Participants
  • addusr

OK, the problem is this:

I have 2 tables in my database (called: test), the first table contains basic user data (id, username, password, salt)

Second table: id, first name, last name, biography, day (birthday), month (birthday), year (year of birth).

When the user logs in, the program prints the user data into the database. When the user enters the login, I need to get the user's data as first and last name, but how to recover this data from the second table?

So far I have developed this:

function get_talkm3_nome () {
    include 'db_connect.php';

    $conn = $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

    $query = "SELECT addusr.nome FROM addusr, members WHERE members.username = '{$_SESSION['username']}'";
    $result = $conn->query($query);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "" . $row["nome"]. "<br>";
    } else {
        echo "Si è verificato un errore, non ho trovato nulla";


This file is stored in a function and I call it to print the name of the actually logged in user, but it doesn't work as expected because instead of getting the current username (my name) I get this: Fabrizio, lorenzo, test3

But how do you solve this problem?



source to share

1 answer

you need 2 tables with a column in each table, this column must have the same value in both tables to establish a relationship between them (foreign key). Then, for a query, you need an inner join to get data from two tables.

Here is an example from w3schools In this example, the foreign key is the column ID.

I recommend you this page for learning web technologies like mysql and php.



All Articles