Note that there are some explanatory texts on larger screens.

plurals
  1. POMySql - three sucessfully joined tables, but failing to get one column result?
    text
    copied!<p>I have three tables (user, post, comments) joined up (for an ajax powered social network thing I am doing for a degree). </p> <p>So far so good but when I loop through them I am getting all the results except one. The reference to who commented on a post (stored within comments table) is coming up only as a number id (which should be joined to the user table but my join is obviously not working correctly!).</p> <p>Any help very much appreciated! Probably something simple as I am still new at this PHP/mysql game!</p> <p>MySQL tables:</p> <pre><code> CREATE TABLE `post` ( `pid` int(10) NOT NULL AUTO_INCREMENT, `uid` int(10) NOT NULL, `post` text NOT NULL, `pid_imageurl` varchar(100) NOT NULL, `likes` int(10) NOT NULL, PRIMARY KEY (`pid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 CREATE TABLE `user` ( `uid` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `password` varchar(20) NOT NULL, `uid_imageurl` varchar(100) NOT NULL, `joindate` varchar(11) NOT NULL, PRIMARY KEY (`uid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; CREATE TABLE `comments` ( `cid` int(10) NOT NULL AUTO_INCREMENT, `comment_pid` int(10) NOT NULL, `comment_uid` int(10) NOT NULL, `comment` text NOT NULL, PRIMARY KEY (`cid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; </code></pre> <p>Code:</p> <pre><code> $sql = "SELECT post.post, post.pid_imageurl, post.likes, user.name, comments.comment, user.uid_imageurl, comments.comment_uid FROM post INNER JOIN user ON post.uid=user.uid LEFT JOIN comments ON comments.comment_pid=post.pid AND user.uid=comments.comment_uid "; $result = mysql_query($sql); while($row=mysql_fetch_assoc($result)){?&gt; &lt;ul&gt; &lt;li&gt;User Profile image here:&lt;? echo $row['uid_imageurl']; ?&gt;&lt;/li&gt; &lt;li&gt;Post:&lt;? echo $row['post']; ?&gt;&lt;/li&gt; &lt;li&gt;Post by:&lt;? echo $row['name']; ?&gt;&lt;/li&gt; &lt;li&gt;Post images:&lt;? echo $row['pid_imageurl']; ?&gt;&lt;/li&gt; &lt;li&gt;post has: &lt;? echo $row['likes']; ?&gt; likes.&lt;/li&gt; &lt;li&gt;&lt;? echo $row['comment']; ?&gt;&lt;/li&gt; &lt;li&gt;by: &lt;? echo $row['comment_uid']; ?&gt;&lt;/li&gt; &lt;!--This is only output as a stored id no rather than users name--!&gt; &lt;/ul&gt; &lt;?} ?&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