how to select data from a second table according to the user

0

How can I select the user who logs in to show the name in php, table 1 is for the main users, and table 2 is for additional users, related to the main user by ID and ID_ppl

I need to show in php the username that starts the session. but only the main user shows me, although I login with an additional user, the additional username must be shown.

For example, I connect with ** Juana ** and in php they show me ** Andres **, the main user; when you should show me the name of Juana

Table 1 main users

id |  usuario  | password | token | nivel | estado | 
----------------------------------------------------
1  | Andres    | *****    | e12A1 | 1     | on     |
----------------------------------------------------

Table 2 additional users

id |  usuario  | password | token | id_ppl | nivel | estado |
-------------------------------------------------------------
1  | Juana     | *****    |       | 1      | 2     | on     |
-------------------------------------------------------------
2  | Martin    | *****    |       | 1      | 2     | off    |
-------------------------------------------------------------

Php query

$stmt = $conn->prepare("SELECT T1.id, T1.usuario, T1.token, T1.nivel, T1.estado FROM escolar AS T1 LEFT OUTER JOIN users_extra AS T2 ON T1.id = T2.id_ppl");

    $stmt->bind_param("ss",$usuario, $password); 
     $stmt->execute(); 
     $stmt->store_result(); 
     if($stmt->num_rows > 0){ 
     $stmt->bind_result($id, $usuario, $token, $nivel, $estado);
     $stmt->fetch();

    $user = array(
     'id' => $id,
     'usuario' => $usuario,
     'token' => $token,
     'nivel' => $nivel,
     'estado' => $estado
     );
    
asked by Cris 30.07.2018 в 06:29
source

1 answer

0

The problem is that in the SELECT you are not selecting the column of the table users_extra but that of the table escolar .

If you want me to show you the column usuario of the second table you must indicate it in SELECT as follows:

$sql="SELECT 
           T1.id, 
           T2.usuario, 
           T1.token, 
           T1.nivel, 
           T1.estado 
FROM escolar AS T1 
LEFT OUTER JOIN users_extra AS T2 ON T1.id = T2.id_ppl";

$stmt = $conn->prepare($sql);

There the second column of SELECT will come from T2 which is the alias of the table users_extra .

    
answered by 30.07.2018 в 07:28