Choose database table for logged in user

0

I want to choose only 1 table from a database, depending on the user who is logged into the website. I want this because I'm making an account management page for site users. On this page I have a calendar (fullcalendar) where I will fetch events from a table.

What I want to do is a table for each user, and arrange a query to make me the select of the right table to show the events of that user only. My current code is selecting all events from a table only:

<?php

 $json = array();

 $requete = "SELECT * FROM eventos ORDER BY id";

 try {
 $bdd = new PDO('mysql:host=localhost;dbname=fullcalendar', 'root', '');
 } catch(Exception $e) {
 exit('Impossivel conectar à base de dados.');
 }

 $resultado = $bdd->query($requete) or die(print_r($bdd->errorInfo()));

 echo json_encode($resultado->fetchAll(PDO::FETCH_ASSOC));

?>
    
asked by anonymous 23.04.2015 / 11:57

1 answer

2

You can solve this by having two tables, one user and one event

User table

| id         | nome        | ...
|------------|-------------|-----
| 1          | joaquim     |         
| 2          | albano      |      
| ...        | ...         |       

Event table

| id         | user_id     | evento             | ...
|------------|-------------|--------------------|----
| 1          | 1           | evento 1 do user 1 | 
| 2          | 1           | evento 2 do user 1 !  
| ...        | ...         | ...                |  

You can do this with a foreign key where user_id is the foreign key of the id of the user.

    
23.04.2015 / 12:08