Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For future reference, here is the final version of this script. I have included a commented section that shows the alternative coding method for the query using the "dot" method.</p> <p>I have tested this script (using both methods) and it functions perfectly.</p> <pre><code>&lt;?php try { $db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db-&gt;exec("SET CHARACTER SET utf8"); // TABLE1 $tablename1 = 'topics'; $topics_col1 = 'TOPIC_NUM'; $topics_col2 = 'TOPIC_NAME'; // TABLE2 $tablename2 = 'people'; $items_col1 = 'NAME'; $items_col2 = 'STATE'; $items_col3 = 'TOPIC'; $items_col4 = 'YEAR'; $items_q = "SELECT $items_col1, $items_col2, $items_col3, $items_col4, $topics_col2 FROM $tablename2 JOIN $tablename1 on ($items_col3 = $topics_col1)"; /* ALTERNATE METHOD FOR QUERY (*No need for all of the vars above using this method.) $items_q = "SELECT $tablename2.NAME, $tablename2.STATE, $tablename2.TOPIC, $tablename2.YEAR, $tablename1.TOPIC_NAME FROM $tablename2 INNER JOIN $tablename1 on ($tablename2.TOPIC = $tablename1.TOPIC_NUM)"; */ $items = $db-&gt;query($items_q); foreach ($items as $items_row) { ?&gt; &lt;h2&gt;&lt;?php echo $items_row[$topics_col2];?&gt;&lt;/h2&gt; &lt;p&gt; &lt;?php echo $items_row[$items_col1];?&gt; &lt;br /&gt; &lt;?php echo $items_row[$items_col2];?&gt; &lt;br /&gt; &lt;?php echo $items_row[$items_col3];?&gt; &lt;br /&gt; &lt;?php echo $items_row[$items_col4];?&gt; &lt;/p&gt; &lt;?php } // end FOREACH $db = null; // close the database connection } // end TRY catch(PDOException $e) { echo '&lt;span class="error"&gt;ERROR:&lt;/span&gt;&lt;br /&gt;'.$e-&gt;getMessage() . "&lt;br /&gt;"; die(); } // end CATCH ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload