Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Query - Help linking two querys into 1 echo statement
    primarykey
    data
    text
    <p>so i have blog posts echoed out in their own 'blogpost' div.</p> <p>I then underneath that have another div which contains the posts comments.</p> <p>I have a table called 'posts' and this contains everything to do with the post. I then have another called... 'comments' which contains information.</p> <p>The way i wanted this to work is that there is a field in 'comments' called updatepostid. And this contains the id of the blogpost that it is associated with.</p> <p>The issue comes when echoing them out. I can get the comments to echo out with something like:</p> <pre><code>$query = 'SELECT * FROM comments'; </code></pre> <p>But as soon as i do e.g. </p> <pre><code>$query = 'SELECT * FROM comments WHERE updatepostid = "'.$postID.'"'; </code></pre> <p>Nothing is shown. I beleive this is because the blogpost and comments are not in the same echo.</p> <p>I am unsure of how to go about getting them into the same echo statement though.</p> <p>So, here is what it looks like currently:</p> <pre><code> &lt;?php $query = 'SELECT * FROM posts WHERE buildID = '.$_GET['id'].' LIMIT '.$limit.''; try { $stmt = $db-&gt;prepare($query); $stmt-&gt;execute(); } catch(PDOException $ex) { die("Failed to run query: " . $ex-&gt;getMessage()); } $rows = $stmt-&gt;fetchAll(); foreach($rows as $row): $postID = $row['postID']; $text = $row['text']; $date = $row['date']; echo basicbbcode (' &lt;div class="blogtest"&gt; &lt;form action="process/updatepost.php" class="updatepost" method="post"&gt; &lt;input type="button" class='.$editenabled.' value="Edit"&gt; &lt;input type="submit" class="saveupdatebutton" value="Save"&gt; &lt;input type="hidden" class="postid" name="postid" value="'.$postID.'"&gt; &lt;div class="text"&gt; &lt;div class="buildtext"&gt;'.$text.'&lt;/div&gt; &lt;div class="editor"&gt;&lt;textarea name="ckeditor"id="ckeditor" class="ckeditor"&gt;'.$text.'&lt;/textarea&gt;&lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; ')?&gt; &lt;?php // Get comments $query = 'SELECT * FROM comments'; try { $stmt = $db-&gt;prepare($query); $stmt-&gt;execute(); } catch(PDOException $ex) { die("Failed to run query: " . $ex-&gt;getMessage()); } $rows = $stmt-&gt;fetchAll(); foreach($rows as $row): $commentID = $row['commentID']; $usercommentID = $row['userID']; $comment = $row['comment']; $updatepostid = $row['updatepostid']; ?&gt; &lt;div class="commentsbox"&gt; &lt;div id="textcomment"&gt;&lt;?php echo "$usercommentID: $comment";?&gt;&lt;/div&gt; &lt;/div&gt; &lt;?php endforeach; ?&gt; &lt;?php endforeach; ?&gt; </code></pre> <p>The above is terrible i know... but its just to show you what im working with.</p> <p>I was thinking about using an INNER JOIN query but still just cant get my head around it.</p> <p>Any help on getting it into one echo statement?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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