The following script works correctly however I don’t like it because it isn’t elegant and is just down right messy.
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.
//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 '<META HTTP-EQUIV="Refresh" Content="0; URL=page_not_found.php">';
} 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'];
}
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.
My most recent attempt is:
$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'];
}
**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…
Thank you **
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 SELECT pages_learn_more plm, pages_tool_tip ptt != SELECT pages_tool_tip ptt, pages_learn_more plm 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)
PIC OF MY DATABASE SCHEME IF IT HELPS

You have a strange mix of implicit (comma-separated tables) and explicit
JOINs, which affects the order the tables appear in. Implicit joins are discouraged, so use only explicit ones.Since you are not using all columns, do not
SELECT *, especially since some tables have the same column names (tool_tip_id), which causes ambiguity. Instead, be a little more explicit about what you’re selecting. You can even replaceplm.*below with the exact list of columns you need from that table.In your while loop, the only columns you access are those of
pages_learn_moreand one frompages_tool_tip, so in theSELECTlist, I usedplm.*for all columns on that table, andptt.tool_tip_linkonly from the other table.Since your primary table appears to be
pages_learn_morewhich may or may not have associated records inpages_to_pages, I substituted aLEFT JOIN, so even if it had no associated tool tip, the query could still return a record (with a NULL tooltip).Technically, the second
LEFT JOINcould just be anINNER JOINsince there must be a record inpages_tool_tipif one existspages_to_pages.