imdead Posted September 7, 2011 Share Posted September 7, 2011 Hey guys, i'm creating a facebook clone. Almost there, got the main/basic features all done, however i've got one piece of code which is really bugging me, don't worry about it being messy and horribley coded. I will be improving and optimising it every chance i get once i've got this down. it's on the main page, i want it to display all recent status' that have been made, i can display them however i think i've bugg'd the SQL somewhere and it's driving my crazy, any help would be greatly appriciated Thanks <?php $sql = mysql_query("SELECT * FROM statuses ORDER BY id DESC"); $sql = mysql_query("SELECT * FROM statuses LEFT JOIN users ON statuses.userid = users.id"); while ($row = mysql_fetch_assoc($sql)) { $status_id = $row['id']; $status = $row['status']; $time_posted = $row['time_posted']; $sql2 = mysql_query("SELECT username,avatar_url FROM users WHERE id=$userid LIMIT 1"); while ($row2=mysql_fetch_array($sql)){ $usernamestatus = $row2['username']; $avatar_url = $row2['avatar_url']; echo '<div id="content">'; echo "<img src='/photos/$usernamestatus/$avatar_url' height='50px' width='50px' />"; echo "<a href=\"profile.php?id=$userid\">". $usernamestatus ."</a>"; echo "<br />"; echo $status; echo " <span class=x><a href=delete_status.php?id=$status_id>x</a></span>"; echo "<br />"; echo time_ago($time_posted); echo "<br />"; echo "<br />"; echo "</div>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/ Share on other sites More sharing options...
JKG Posted September 7, 2011 Share Posted September 7, 2011 facebook clone... right.... why are you running 3 sql queries? if you get 0.01% of the traffic facebook gets you will be bombing out your mysql server. you getting an error? Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266486 Share on other sites More sharing options...
ManiacDan Posted September 7, 2011 Share Posted September 7, 2011 You absolutely should never do SQL inside of a loop, a JOIN is almost always the correct replacement. In fact, you've DONE your join, yet you still do unnecessary queries inside your loop. print_r($row) to see what the contents are. Maybe you're doing it wrong. There's nothing here to indicate that your array indicies are correct. If you're making a social network for the fun of it and to learn, it's a good place to start. Lots of complex relationships in a social network. If you're doing it to actually get traffic or make a business, stop right now. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266492 Share on other sites More sharing options...
JasonLewis Posted September 7, 2011 Share Posted September 7, 2011 How do you propose to gain a foothold on Facebook if you're making a clone? No one wants another Facebook. I'd stop making this clone if I were you and make something better than Facebook. Unless you're just screwing around, in which case go nuts. Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266508 Share on other sites More sharing options...
ManiacDan Posted September 7, 2011 Share Posted September 7, 2011 How do you propose to gain a foothold on Facebook if you're making a clone? No one wants another Facebook. I'd stop making this clone if I were you and make something better than Facebook. I worked at a social network for the last couple years. It's possible to exist alongside facebook, but you need tens of millions of dollars and at least a dozen developers, hopefully more. Plus, advertising, compelling content, and lots and lots of luck. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266520 Share on other sites More sharing options...
imdead Posted September 7, 2011 Author Share Posted September 7, 2011 Guys, i'm just doing this on my localhost to see if i can build anything remotely like facebook. For the fun and it's going pretty well , but yeah i know that it's all mucky and horrible. I'm not getting any errors, however it's only displaying 1status, instead of the 100's that are there. Thankyou for your help so far guys. Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266526 Share on other sites More sharing options...
imdead Posted September 7, 2011 Author Share Posted September 7, 2011 Right this is what i've got atm, <?php $sql = mysql_query("SELECT * FROM statuses LEFT JOIN users ON statuses.userid = users.id"); while ($row = mysql_fetch_assoc($sql)) { $status_id = $row['id']; $status = $row['status']; $time_posted = $row['time_posted']; $sql2 = mysql_query("SELECT username,avatar_url FROM users WHERE id=$userid LIMIT 1"); while ($row2=mysql_fetch_array($sql2)){ $usernamestatus = $row2['username']; $avatar_url = $row2['avatar_url']; print_r($row); print"<br />"; print_r($row2); echo '<div id="content">'; echo "<img src='/photos/$usernamestatus/$avatar_url' height='50px' width='50px' />"; echo "<a href=\"profile.php?id=$userid\">". $usernamestatus ."</a>"; echo "<br />"; echo $status; echo " <span class=x><a href=delete_status.php?id=$status_id>x</a></span>"; echo "<br />"; echo time_ago($time_posted); echo "<br />"; echo "<br />"; echo "</div>"; } } } ?> Array ( [id] => 1 [status] => test [userid] => 1 [time_posted] => 18:42:36 [username] => ****** [password] => ****** [email] => **** [pm_count] => 1 [avatar_url] => **** [about_me] => [interested] => Women [relationship] => In a Relationship [gender] => Male ) Array ( [0] => ****** [username] => ***** [1] => ***** [avatar_url] =>***** ) From what i can see, there is some serious bug in here. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266532 Share on other sites More sharing options...
imdead Posted September 7, 2011 Author Share Posted September 7, 2011 No worries everyone! i fixed it myself <?php $sql = mysql_query("SELECT * FROM statuses LEFT JOIN users ON statuses.userid = users.id"); while ($row = mysql_fetch_assoc($sql)) { $status_id = $row['id']; $status = $row['status']; $time_posted = $row['time_posted']; $userid = $row['userid']; $sql2 = mysql_query("SELECT username,avatar_url FROM users WHERE id=$userid LIMIT 1"); while ($row2=mysql_fetch_array($sql2)){ $usernamestatus = $row2['username']; $avatar_url = $row2['avatar_url']; print_r($row); print_r($row2); echo '<div id="content">'; echo "<img src='/photos/$usernamestatus/$avatar_url' height='50px' width='50px' />"; echo "<a href=\"profile.php?id=$userid\">". $usernamestatus ."</a>"; echo "<br />"; echo $status; echo " <span class=x><a href=delete_status.php?id=$status_id>x</a></span>"; echo "<br />"; echo time_ago($time_posted); echo "<br />"; echo "<br />"; echo "</div>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266536 Share on other sites More sharing options...
ManiacDan Posted September 7, 2011 Share Posted September 7, 2011 There is absolutely no reason for your inner query and loop at all. Your outer query already fetches all the data from the user table, don't make another round trip to the database for every status. Quote Link to comment https://forums.phpfreaks.com/topic/246637-facebook-clone-help/#findComment-1266547 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.