Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP, MYSQL noob joining(?) tables specifics
    text
    copied!<p>I am new to PHP and am making a social network as practice and to apply what I have learned in the "real world". Anyhow, I have two tables in a MySQL database that i am trying to display on my site in the same html table that is being rendered through an php echo.</p> <p>here are the tables</p> <p>(table1) note_system: -id, -username, -note</p> <p>(table2) comments: -id, -cid (equals id from note_system), -username, -comment</p> <p>so someone makes a post and it saves to the note_system table then someone comments on the post and it saves to the comment table with the id from the note_system table so a relation can be established.</p> <p>So what I am trying to do is get the post comments to display with the relevant post. I have gathered that I need maybe a JOIN or UNION to make this happen but I am at a complete loss on how to do it. Been racking my brain and doing tons of google searches but I am not really getting anywhere. Everything I try gives me errors. The Notes display just fine and as intended but I can't for the life of me figure out how to get the comments to show up there too.</p> <p>Here is my code (don't laugh at the noob-ness of my PHP, this is my 2nd PHP program ever and I obviously have much to learn, I would like to clean it up at some point but for now I just want it to be functional)</p> <pre><code>&lt;?php // Display Note_Wire $con=mysqli_connect($host,$username,$password,$dbname); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //format and display the Note_Wire results with comments $result = mysqli_query($con,"SELECT * FROM note_system"); while($row = mysqli_fetch_array($result)) { echo "&lt;center&gt;"; echo "&lt;table class='note_wire'&gt;"; echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $row['username'] . "&lt;/td&gt;" ; echo "&lt;/tr&gt;&lt;tr&gt;"; echo "&lt;td&gt;&lt;a href=''&gt;vote up&lt;/a&gt;" . " " . $row['rank'] . " " . "&lt;a href=''&gt;vote down&lt;/a&gt;&lt;/td&gt;" ; echo "&lt;/tr&gt;&lt;tr&gt;"; echo "&lt;td&gt; &lt;a href='{$row['link']}' target='blank'&gt;{$row['link']}&lt;/a&gt;"; echo "&lt;/tr&gt;&lt;tr&gt;"; echo "&lt;td&gt;" . $row['note'] . "&lt;/td&gt;" ; echo "&lt;/tr&gt; "; //add comments attempt this is where I would like the comments to be displayed echo ' &lt;td&gt;&lt;form action="add_comment.php" method="POST"&gt; &lt;input type="hidden" name="username" value="'; echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); echo '" /&gt;'; echo '&lt;input type="hidden" name="cid" value="'; echo $row['id']; echo '" /&gt;'; echo '&lt;textarea name="comment"&gt;comment...&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="comment" /&gt; &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt; '; echo "&lt;/table&gt;"; // break before next note-wire record renders echo "&lt;br /&gt;"; } echo "&lt;/center&gt;"; ?&gt; </code></pre> <p>I hope my chicken scratch programming makes sense. Thanks for your time and knowledge. </p>
 

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