Linking tables from sql queries leads to errors.

I have two tables in sql and when I iterate over them in php I get a weird result ... The desired effect is to echo the intro input from the intro table and the msg from the table messages and then order them by date.

$result = mysql_query("SELECT intro.user_id, intro.date, intro.message_id, intro.intro FROM intro WHERE user_id = {$uid}
                    UNION SELECT messages.user_id, messages.msg, messages.message_id, messages.date FROM messages
                    ORDER BY date DESC ");




while($row = mysql_fetch_array($result))
  {
  echo  "<p>".getElapsedTime($row['date'])."</p>
  <strong>></strong> <a href=\"outro.php?msg_id=".$row['intro.message_id'].
  "\">".$row['intro'] . "</a><br>";
  }

      


Intro table

      

enter image description here

Messages table

      

enter image description here

enter image description here

However, I am getting this strange result as shown above. The top displaying only dates is drawn from the table messages

. I don't know why this is happening or how to fix it.

+3


source to share


1 answer


Your UNION is flawed. Try:



SELECT intro.user_id, intro.date, intro.message_id, intro.intro
FROM intro WHERE user_id = {$uid}
UNION
SELECT messages.user_id, messages.date, messages.message_id, messages.msg
FROM messages
ORDER BY date DESC

      

+2


source







All Articles