title: PHP MySQL query using UNIONs and locating which table the data came from
body: <p>So I have a query that I am selecting values from two different tables and UNIONizing them together. I need to know where which entry came from as I need to form a different URL for each. What is the best way and proper syntax to tag both of these SELECT queries. Any help would be appreciated. Code to follow:</p>
  
  <pre><code>    $query = "SELECT newsletter_id id, newstitle title FROM tbl_news WHERE (newstitle LIKE '%".stripslashes($rowCont-&gt;fname)."%' &amp;&amp; newstitle LIKE '%".stripslashes($rowCont-&gt;lname)."%') || (newsletter_content LIKE '%".stripslashes($rowCont-&gt;fname)."%' &amp;&amp; newsletter_content LIKE '%".stripslashes($rowCont-&gt;lname)."%') SET @type = news
      UNION
      SELECT event_id id, event_title title FROM tbl_event WHERE ((event_title LIKE '%".stripslashes($rowCont-&gt;fname)."%' &amp;&amp; event_title LIKE '%".stripslashes($rowCont-&gt;lname)."%') || (event_content LIKE '%".stripslashes($rowCont-&gt;fname)."%' &amp;&amp; event_content LIKE '%".stripslashes($rowCont-&gt;lname)."%')) SET @type = event";
  
      $result = mysql_query($query) or die(mysql_error());
  
      $i = 0;
  
      while($row = mysql_fetch_assoc($result)) {
  
          if($table_name == table1) { //SIMULATED TABLE NAME VARIABLE IF STATEMENT, NEED TO KNOW HOW TO GET TABLE NAME FOR SORTING
              echo "&lt;p&gt;&lt;a style='color:#00aeef' href='http://www.url1.com/display.php?article_=".$row["id"]."'&gt;".$row["title"]."&lt;/a&gt;&lt;/p&gt;";
          } else {
              echo "&lt;p&gt;&lt;a style='color:#00aeef' href='http://www.url1.com/display.php?article_=".$row["id"]."'&gt;".$row["title"]."&lt;/a&gt;&lt;/p&gt;";
          }
      }
  </code></pre>