Pull only user items

Asked

Viewed 30 times

-1

Talk personal, I have 1 database and data with 2 tables so far the first table is responsible for registering the users with the name "users", the other table is where I want to register the events in Full_calendar with JS, my current code can pull everything that is there, but I would like to appear the indidual events of each user.

Follow my PHP search code:


<?php

include 'conexao.php';

$query_events = "SELECT id, title, color, start, end, usuario FROM events";


$resultado_events = $conn->prepare($query_events);
$resultado_events->execute();

$eventos = [];

while($row_events = $resultado_events->fetch(PDO::FETCH_ASSOC)){
    $id = $row_events['id'];
    $title = $row_events['title'];
    $color = $row_events['color'];
    $start = $row_events['start'];
    $end = $row_events['end'];
    
    $eventos[] = [
        'id' => $id, 
        'title' => $title, 
        'color' => $color, 
        'start' => $start, 
        'end' => $end, 
        ];
}

echo json_encode($eventos);


?>

and the information in the tables:

Events = id, title, color, start, end, user

user = id, name, user, email, password

I would like to show in the calendar only what is of each user when he logs in, the login system is already working

1 answer

0

You must use the clause WHERE in your event query query to filter by authenticated user.

Assuming your user is on $_SESSION in the user index, and the value stored in this variable is only your id, and also that the column usuario on the table events is the user foreign key; add the following line to concatenate the filter:

$query_events = "SELECT id, title, color, start, end, usuario FROM events ";
$query_events .= " WHERE usuario = '".$_SESSION['usuario']."'";

The rest of your code apparently won’t need any more changes.

Quick explanation of the clause

The clause WHERE is read QUANDO, then reading SQL would be something close to: "SELECT id, title, color, start, end, TABLE USER Events WHEN user = value"

  • thanks for the help, but not searching, I am not able to create $_SESSION['user'] when logging in, I have this code:

  • I think I’ll filter by ID, but I wanted by user

  • If you put the code that records the authenticated user in the session helps me to help you, at first I am making assumptions, when said user I referred to any value q vc is using as a primary and foreign key. If you are using the user name, just make the adjustments in the sample code

Browser other questions tagged

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