I have the following query to two tables with UNION
. The problem is that when trying to print any data from the 2nd query it says undefine X-field , but the one from the first query does print me data.
This is my code:
<?php
$query=mysqli_query($conexion,
"SELECT de,para,nombre,apellido,fecha,avatar,post_id FROM comentarios
WHERE para='$id'
UNION ALL
SELECT de,para,nombre_e,apellido_e,fecha_e,avatar_e,id_post FROM notificaciones
WHERE para='$id'
ORDER BY GREATEST(fecha,fecha )DESC");
Then I print data with while
:
if ($query) {
while($row=mysqli_fetch_array($query)){
echo $row["nombre"];//imrprime nombre de la primera consulta sussccefully
echo $row["nombre_e"];//me dice undefined index nombre_e
}
}
I do not understand why or why the error is due.
Thanks @OscarGarcia for understanding my problem and what I try to do the typical bell with a notification system therefore try to show two tables in the same content sorted by date for comment notifications and evaluations I am sure that this will serve as a guide in the future since it is one of the codes that I have had the most problems show two tables together in order of date , returning to the subject your code gave me the following results ..
with the first fragment of code works but I get content in white bone empty and it shows me just as I do not know what is due and with the second code fragment I get the same undefined index X-field Thank you for taking the time to help me. I hope you can continue to help me and clarify these doubts.