Query with duplicate data in Mysqli

Asked

Viewed 105 times

0

Hello.

I’m having trouble displaying results of a bank consultation.

I have 2 tables

Tabela 1 - logs // logs do sistema
//user_id = são os usuários administrativos do sistema
//user_perfil = é o perfil do usuário que usa o sistema (cliente).

    id | user_id | user_perfil
    1  |    10   |   105
    2  |    10   |   106
    3  |    12   |   105
    4  |    12   |   104

    Tabela 2 - user
    id  | nome
    10  | Luiz
    11  | Renato
    12  | Marcio

I have a screen that displays the result through the customer profile ID, by GET, which searches the information through another table that is the user profile, which displays the name and other information.

The detail here is to display the administrators access log, to know who has already viewed this profile. For this I created the table logs, which every time the administrator accesses this profile saves the user_id - of the administrator and user_profile - of the client profile. records only, without repeating.

The logic would be as follows, through GET generates the profile ID, and saves in the database logs, administrator id (Session) and the profile ID Get.

Display only who saw, in my case with while it is displaying all administrators, I used GROUP BY, not to repeat the data.

I would like to display the name of the administrator who viewed the user profile. I tried to do the while below, but it’s not rolling. No error occurs, it displays only the name of each administrator. Type user Luiz viewed the profile 105 and 106, and Marcio viewed 105 and 104, when accessing the profile 105 should display Luiz and Marcio, and profile 106 only Luiz, but in all profile is displaying Luiz, Marcio and Renato.

Can someone help me? Thank you

Follow the while I did:

        $situacao_atual = $row_logs['user_id'];
        $result_logs = "SELECT * FROM user WHERE id = '$situacao_atual'";
        $result_logs = mysqli_query($conn, $result_logs);

        $row_user = mysqli_fetch_assoc($result_logs);
        //$row_logs = mysqli_fetch_assoc($result_logs);

        $iduser = $row_logs['user_perfil'];
          if($idcurriculo = $id){

                echo $row_usuarios['nome'];
            }
            else {echo "no";}        
 }

  • 2

    Because it’s running twice the $row_user = mysqli_fetch_assoc($result_logs); $row_logs = mysqli_fetch_assoc($result_logs);?

  • Hello, comment error, should be commented the $row_logs, will be edited the question. thanks

1 answer

0

the problem is in your Select use this select that solves your problem of bringing the names that viewed a particular profile

select nome from tab_log l where user_id = ?parametro join tab_user u on u.id = l.user_id
  • Unfortunately it didn’t work.

  • 1

    Solved as follows, modified the query and is working correctly. $sql = "SELECT * FROM logs WHERE curriculo_id = $id";
 $result_log = mysqli_query($conn, $sql);
 while($row_logs = mysqli_fetch_assoc($result_log)){
 $situacao_log = $row_logs['usuario_id'];
 $resultado_nome = "SELECT * FROM usuarios WHERE id = '$situacao_log'";
 $result_nome = mysqli_query($conn, $resultado_nome);
 $row_nome = mysqli_fetch_assoc($result_nome);

  • Great, I know well SQL but I do not know PHP but if solved your problem great, congratulations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.