I am trying to create a query that only selects the data that has the respective userID of my database, but when I send the query the query brings me all the data, I am not very expert in database
My query is the following:
$query = "SELECT * FROM messages WHERE idEmitter
IN ($us, $msg[0]) AND idReceiver IN ($msg[0], $us) ORDER BY sent ASC" ;
The variable $us
is assigned at the beginning of the session, and the variable $msg
is in a foreach
What I want to do is bring me all the data that comes in my database depending on the id that I bring in the url
localhost / maybe% 20algo / chat / index.php? id = 4
This is what I should bring from my database
However, in my message system it brings all the data and should only bring those that have a name fernando
In my database, I have the following data in each row
My purpose is to create a private chat, in my first image I show the data of demon, cease and fernando, but as I just mentioned, I want a private chat, that is:
I want you to bring the messages you have in idEmitter and idReceiver depending on the id's they have
1 con 3
3 con 1
3 con 4
4 con 3
I hope you gave me to explain
This is the code of my foreach, and where all the chat is shown
<?php session_start();
include 'db.php';
require_once '../functions.php';
$emit = obtener_mensajes($conexion, $us);
comprobarSession();
?>
<?php foreach ($emit as $msg): ?>
<?php
$query = "SELECT * FROM messages WHERE idEmitter IN ($us) AND idReceiver IN ($msg[0], $us) ORDER BY sent ASC" ;
$run = $conexion->query($query);
while ($row = $run->fetch(PDO::FETCH_ASSOC)):
//var_dump($row);
?>
<div id="chat_data">
<span style="color: green;"><?php echo $row['nombre']; ?> </span><br>
<span style="color: brown;"><?php echo $row['message']; ?></span>
<span style="float: right;"><?php echo fecha($row['sent']); ?></span>
</div>
<?php endwhile;?>
<?php endforeach ?>
Edition
The way I show the first notification where I printed the last message is the following
function obtener_mensajes($conexion, $us) {
$statement = $conexion->prepare("SELECT SQL_CALC_FOUND_ROWS m.idEmitter, m.nombre, m.message, m.sent, m.idReceiver
FROM messages m
INNER JOIN (SELECT idEmitter, idReceiver, max(idChat) AS idChat
FROM messages
WHERE seenEmpresa = 0
GROUP BY idEmitter, idReceiver) mm
ON m.idChat = mm.idChat
AND m.idEmitter = mm.idEmitter
AND m.idReceiver = mm.idReceiver
WHERE m.idReceiver = $us
AND seenEmpresa = 0");
$statement->execute();
return $statement->fetchAll();
}
later I use a file called notifiaciones.view.php
to show the notification and get all the data of the previous function
<?php session_start();
require 'extras/config.php';
require 'functions.php';
comprobarSession();
$conexion = conexion($bd_config);
if (!$conexion) {
header('Location: error.php');
}
$mensajes = obtener_mensajes($conexion, $us);
if (!$mensajes) {
$vacio = "<li>No cuentas con mensajes nuevos</li>";
$guardadas = "<li>Los mensajes se han guardado en chats guardados</li>";
}
require "views/mensajes.view.php";
Next print all the data that the $mensajes
variable brings and print them in an html
<?php foreach ($mensajes as $mensa): ?>
muchos mas div's
<input type="hidden" value="<?php echo $mensa['idEmitter']; ?>"><a href="<?php echo RUTA; ?>/chat/index.php?id=<?php echo $mensa['idEmitter']; ?>">
muchos div's
</div><hr>
<?php endforeach;?>
and that's how I send the id of the sender through the url and in the chat section I want to print it
<?php session_start();
include 'db.php';
require_once '../functions.php';
require_once '../single.mensaje.php';
comprobarSession();
?>
<?php
$query = "SELECT ue.idUsuario, ue.nombre, ur.nombre, c.sent, c.message FROM messages c
INNER JOIN usuarios ue ON c.idEmitter = ue.idUsuario
INNER JOIN usuarios ur ON c.idReceiver = ur.idUsuario
WHERE (c.idEmitter = **AQUI QUIERO IMPRIMIR LA ID** AND c.idReceiver = $us)
or (c.idEmitter = $us AND c.idReceiver = **AQUI QUIERO IMPRIMIR LA ID**)";
$run = $conexion->query($query);
while ($row = $run->fetch(PDO::FETCH_ASSOC)):
var_dump($row);
?>
<div id="chat_data">
<span style="color: green;"><?php echo $row['nombre']; ?> </span><br>
<span style="color: brown;"><?php echo $row['message']; ?></span>
<span style="float: right;"><?php echo fecha($row['sent']); ?></span>
</div>
<?php endwhile;?>
How I try to pick up the id from the url
This is the way I try to get the id from the url
<?php session_start();
include 'db.php';
require_once '../functions.php';
comprobarSession();
$idEmitter = $_POST['id']; <-- id de la url
?>
<?php
$query = "SELECT ue.idUsuario, ue.nombre, ur.nombre, c.sent, c.message FROM messages c
INNER JOIN usuarios ue ON c.idEmitter = ue.idUsuario
INNER JOIN usuarios ur ON c.idReceiver = ur.idUsuario
WHERE (c.idEmitter = $idEmitter <-- variable que contiene el id de la url previamente declarado AND c.idReceiver = $us)
or (c.idEmitter = $us AND c.idReceiver = $idEmitter)";
$run = $conexion->query($query);
while ($row = $run->fetch(PDO::FETCH_ASSOC)):
var_dump($row);
?>
<div id="chat_data">
<span style="color: green;"><?php echo $row['nombre']; ?> </span><br>
<span style="color: brown;"><?php echo $row['message']; ?></span>
<span style="float: right;"><?php echo fecha($row['sent']); ?></span>
</div>
<?php endwhile;?>
Error that marks me in the chat
Notice: Undefined index: id in C: \ xampp \ htdocs \ maybe company \ chat \ chat.php on line 8 NULL Fatal error: Uncaught Error: Call to a member function fetch () on boolean in C: \ xampp \ htdocs \ maybe company \ chat \ chat.php: 25 Stack trace: # 0 {main} thrown in C: \ xampp \ htdocs \ maybe company \ chat \ chat.php on line 25