title: PHP Sql query to multiple tables in MYSQL database - Needs professional touch
body: <p>The following script works correctly however I don't like it because it isn't elegant and is just down right messy. </p>
  
  <p>Without getting too advanced (meaning I don't want OOP style) I just want to minimize this code in a procedural fashion. Mainly I want to combine the SQL queries and clean up the mess.</p>
  
  <pre><code>//get page url and query db to find the correct page
  $this_page = $_GET['page'];
  $this_page = escape_data($_GET['page']);
  
  //Make sure page exists - if it doesn't redirect the browser
  $SQL_page_exist = "SELECT * FROM pages_learn_more WHERE page_title = '$this_page'";
  $SPE_result = mysql_query($SQL_page_exist);
  
  while ($details = mysql_fetch_array($SPE_result))
  {
  $page_id          =     $details['id'];
  $page_title       =     $details['page_title'];
  $main_title       =     $details['main_title'];
  $main_content     =     $details['main_content'];
  $sub_title        =     $details['sub_title'];
  $sub_content      =     $details['sub_content'];    
  }
  
  if(mysql_num_rows($SPE_result) == 0) 
  {
  echo '&lt;META HTTP-EQUIV="Refresh" Content="0; URL=page_not_found.php"&gt;';
  
  } else {
  
  $SQL = 
      "SELECT   ptt.tool_tip_link
         FROM   pages_tool_tip ptt
         JOIN   pages_to_pages ptp
           ON   ptp.tool_tip_id = ptt.tool_tip_id
        WHERE   ptp.learn_more_id = '$page_id'";
  
    $result = mysql_query($SQL); // or die(mysql_error()); 
  
    //set array for reference_keys variable which may contain 0 to 20 keys
    $reference_keys = array();
    while ($db_field = mysql_fetch_array($result))
  {   
   $reference_keys[] =     $db_field['tool_tip_link'];    
  }   
  </code></pre>
  
  <p>Ok so this above code works as it should, but it is very ugly and I have been trying for the past 2 hours to refine it into something clean, simple and beautiful, but alas my skills are not yet there and all i get is mysql errors.</p>
  
  <p>My most recent attempt is:</p>
  
  <pre><code>$SQL = 
  "
       SELECT     *
         FROM     pages_learn_more plm
  
         JOIN     pages_tool_tip ptt, pages_to_pages ptp 
           ON     ptp.tool_tip_id = ptt.tool_tip_id
        WHERE     plm.page_title = '$this_page' AND ptp.learn_more_id = plm.id 
  ";
  
  $result = mysql_query($SQL); // or die(mysql_error()); 
  
  $reference_keys = array();
  while ($details = mysql_fetch_array($result))
  {   
          $reference_keys[] =     $details['tool_tip_link'];  
          $page_id          =     $details['id'];
          $page_title       =     $details['page_title'];
          $main_title       =     $details['main_title'];
          $main_content     =     $details['main_content'];
          $sub_title        =     $details['sub_title'];
          $sub_content      =     $details['sub_content'];    
  }
  </code></pre>
  
  <p>**This is like my 30th failed attempt to clean up this code. Can someone with knowledge please help me out and if possible please explain your input...</p>
  
  <p>Thank you ** </p>
  
  <p>As a side note:
  I even tried many queries in PHPmyAdmin, which taught me that the order of your tables in your SELECT of FROM statement will impact your result so for example <code>SELECT pages_learn_more plm, pages_tool_tip ptt</code> !=  <code>SELECT pages_tool_tip ptt, pages_learn_more plm</code>  And I don't understand how the order of these statements impacts the results. (When used in the full query 1 Will result in an error and the other Shows the tables)</p>
  
  <p>PIC OF MY DATABASE SCHEME IF IT HELPS</p>
  
  <p><img src="https://i.stack.imgur.com/RxcD9.png" alt="enter image description here"></p>