I'm doing a Status post, to make publications.
I have two tables:
- people.
- posts.
What I want is, when I enter a user's session, I get their respective publications. And the only thing that comes out is a single one, and that same one comes out in the other users. How do I filter according to the logged in user and, of course, get all their posts?
This is how I'm trying to get the logs:
<?php
$query_buscar_posts = mysqli_query($conn, "SELECT * FROM posts LEFT JOIN
personas ON posts.User_Id = personas.Id") or die('Error: ' .
mysqli_error($conn));
while ($posts_filas = mysqli_fetch_array($query_buscar_posts)) {
$code = $posts_filas['Id'];
$user_post_Id = $posts_filas['User_Id'];
$title = $posts_filas['Titulo'];
$contenido = $posts_filas['Contenido'];
}
?>
<?php
echo $title;
echo $contenido;
?>
I have the ID of the logged in user saved in the variable $my_id . I get this from the file that verifies my session.
This is the file:
<?php
include('conexion.php');
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($conn,"select * from personas where correo =
'$user_check' ");
$fila = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$my_id = $fila['Id'];
$login_session = $fila['correo'];
$nombre = $fila['nombre'];
$apellido = $fila['apellido'];
$Avatar = $fila['Fotos'];
if(!isset($_SESSION['login_user'])){
header("location:start.php");
}
?>
We can iterate through the array of results as follows. We will use
a tabledivs (at the author's request) to display the data more ordered:Notice that we use the comparison with the id directly in
WHERE User_Id = $my_id
. Also, because of the data you are using, it is not necessary to perform the JOIN with the tablepersonas
.On the other hand, to print the values, we use
htmlentities()
.However, if you wanted to use data from the table
personas
, the JOIN would be used, just like you were trying to do.Lastly, the code you are using to get the id of the logged in user presents a serious security problem. You should read How to prevent SQL injection in PHP? and use a prepared statement in your first query.
Proof: