Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP MySQL Printing User Field from Joined Table
    text
    copied!<p>I almost have this but cannot figure out the last step! From two tables I have matched up <code>post_id</code> with <code>username</code>, so now I just want to print the name of the user under the title of the post with the loop below. The <code>$joined</code> query generates the below table... but how do I match the specific user to the post? Let me know if more clarification is needed </p> <pre><code>function get_joined() { $sql = "SELECT `posts`.`post_id` AS `post_id`, `posts`.`post_date` AS `post_date`, `posts`.`post_title` AS `post_title`, `posts`.`post_body` AS `post_body`, `users`.`id` AS `user_id`, `users`.`username` AS `user_name` FROM `posts` LEFT JOIN `users` ON `users`.`id` = `posts`.`user_id` ORDER BY `post_date` DESC"; $joined = mysql_query($sql); return $joined; } </code></pre> <p>Printing the post, my issue is at <code>&lt;?php echo $joined['username']; ?&gt;</code></p> <pre><code>&lt;?php $posts = get_posts(); foreach($posts as $post) { ?&gt; &lt;h2&gt;&lt;a href ="blog_read.php?pid=&lt;?php echo $post['id']; ?&gt;"&gt;&lt;?php echo $post['title']; ?&gt;&lt;/a&gt;&lt;/h2&gt; &lt;h4&gt;By &lt;font color="#FF6347"&gt;&lt;?php echo $posts['user_name']; ?&gt;&lt;/a&gt;&lt;/font&gt; on &lt;?php echo $post['date']; ?&gt;&lt;/h4&gt; &lt;h4&gt;&lt;?php echo $post['total_comments']; ?&gt; comments, last comment &lt;?php echo $post['last_comment']; ?&gt; &lt;hr /&gt; &lt;p&gt;&lt;?php echo $post['preview']; ?&gt;...&lt;/p&gt; &lt;?php } ?&gt; </code></pre> <p>Users Table:<br> <code>id|username</code><br> <code>1 | steve</code><br> <code>2 | jon</code><br> <code>3 | mike</code> etc</p> <p>Posts Table:<br> <code>post_id|post_user|post_tite|post_body|post_date</code><br> <code>1 | steve</code> <code>title</code> <code>body</code> <code>date</code><br> <code>2 | steve</code> <code>title</code> <code>body</code> <code>date</code><br> <code>3 | steve</code> <code>title</code> <code>body</code> <code>date</code><br> etc</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