Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking tables from sql, query yields errors.

I have two tables in sql and when I echo them out in php I get a weird result... The desired effect is to echo out intro from table intro and msg from table messages- then organize 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'm getting this weird result as displayed above. The top portion that are just dates echoing out is from the table messages. I don't know why this is happening nor how to fix it.

like image 618
uneducatedguy Avatar asked Apr 09 '26 05:04

uneducatedguy


1 Answers

Your UNION is messed up. 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
like image 105
webbiedave Avatar answered Apr 11 '26 17:04

webbiedave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!