desjardins2010 Posted December 7, 2010 Share Posted December 7, 2010 Hey Again; trying to have the code select the top 5 players bases on exper field in the DB 'SELECT * from players WHERE exper = ''; Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 7, 2010 Share Posted December 7, 2010 look into ORDER BY and LIMIT. if exper is, say, a numeric value you can have it sort by the value and limit the returned records to 5 Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted December 7, 2010 Share Posted December 7, 2010 SELECT * from players ORDER BY experience DESC LIMIT 5 Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 7, 2010 Author Share Posted December 7, 2010 'SELECT * from players ORDER BY exper DESC LIMIT 5; if thats what you want me to try that don;t work Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 7, 2010 Share Posted December 7, 2010 what error or output are you getting? ("don't work" doesn't supply us enough info) Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 7, 2010 Share Posted December 7, 2010 What do you mean by it "don't work"? What is it doing or not doing that it should or should not do? Quote Link to comment Share on other sites More sharing options...
shlumph Posted December 7, 2010 Share Posted December 7, 2010 Does it sit on the couch all day, eating nachos, drinking beer, and ignoring the classifieds? Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 7, 2010 Author Share Posted December 7, 2010 sorry bout that, here is the code <?php if (!$link = mysql_connect('localhost', 'root', '')) { echo 'Could not connect to mysql'; exit; } if (!mysql_select_db('tutorial')) { echo 'Could not select database'; exit; } $sql = "SELECT * FROM players ORDER BY exper DESC LIMIT 5; $result = mysql_query($sql, $link); if (!$result) { echo "DB Error, could not query the database\n"; echo 'MySQL Error: ' . mysql_error(); exit; } ?> <H2>TOP PLAYERS BY EXPERIENCE</H2><BR> <?php while ($row = mysql_fetch_assoc($result)) { echo "<table border=\"1\" align=\"left\">"; echo "<tr>{$row['name']}</tr>"; } mysql_free_result($result); ?> Now it should be putting out the names of the players with the top 5 highest experience but it's returning an error.. Parse error: parse error in C:\wamp\www\test.php on line 23 Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted December 7, 2010 Share Posted December 7, 2010 $sql = "SELECT * FROM players ORDER BY exper DESC LIMIT 5; Should be $sql = "SELECT * FROM players ORDER BY exper DESC LIMIT 5"; Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 7, 2010 Author Share Posted December 7, 2010 Ahhh shoots ok yes that would be it.. that worked now however the output is all in a row I have tried this while ($row = mysql_fetch_assoc($result)) { echo "<table border=\"1\" align=\"left\">"; echo "<tr>{$row['name']}</tr>"."<BR>"; // notice the <BR> but that didn't do it that gave me this solidsouljasonharrylawrencetom Quote Link to comment Share on other sites More sharing options...
Rifts Posted December 7, 2010 Share Posted December 7, 2010 echo "<table border=\"1\" align=\"left\">"; while ($row = mysql_fetch_assoc($result)) { echo "<tr>{$row['name']}</tr>"; } echo "</tr></table>"; Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 7, 2010 Share Posted December 7, 2010 echo "<table border=\"1\" align=\"left\">"; while ($row = mysql_fetch_assoc($result)) { echo "<tr><td>{$row['name']}</td></tr>"; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 7, 2010 Author Share Posted December 7, 2010 I dunno I don't get it makes sence to me but getting this error: Notice: Use of undefined constant count - assumed 'count' in E:\WAMP\www\index.php on line 5 code is here <?php if (!$link = mysql_connect('localhost', 'root', '')) { echo 'Could not connect to mysql'; exit; } if (!mysql_select_db('tutorial')) { echo 'Could not select database'; exit; } $sql = "SELECT * FROM players ORDER BY exper DESC LIMIT 5"; $result = mysql_query($sql, $link); if (!$result) { echo "DB Error, could not query the database\n"; echo 'MySQL Error: ' . mysql_error(); exit; } ?> <H2>TOP PLAYERS BY EXPERIENCE</H2><BR> <?php while ($row = mysql_fetch_assoc($result)) { echo "<table border=\"1\" align=\"left\">"; echo "<tr><td>{$row['name']}</td></tr>"; } echo "</table>"; mysql_free_result($result); ?> Quote Link to comment Share on other sites More sharing options...
rondog Posted December 7, 2010 Share Posted December 7, 2010 From now on please wrap you code by hitting the php icon when posting. Also, I dont see a constant count. This file isn't index.php is it? Line 5 of this is an echo.. Quote Link to comment Share on other sites More sharing options...
MargateSteve Posted December 8, 2010 Share Posted December 8, 2010 Not that it is affecting the problem you mention, shouldn't echo "<table border=\"1\" align=\"left\">"; come before the 'while' statement? Steve Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 8, 2010 Author Share Posted December 8, 2010 <H2>TOP PLAYERS BY EXPERIENCE</H2><BR> <?php while ($row = mysql_fetch_assoc($result)) { echo "<table border=\"1\" align=\"left\">"; echo "<tr><th>Player</th><th>Experience</th></tr><tr align=\"center\"><td>john doe</td><td>894</td></tr>"; } echo "</table>"; so this is working however not given me the results I was hoping for.. what this is doing is drawing a new row for each what I'm looking for is simply a table with two bold headers one saying PLAYER the other saying EXPERIENCE and then each record in a col under it any ideas? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 8, 2010 Share Posted December 8, 2010 Anything within the while() loop will repeat for each record retrieved. Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 8, 2010 Author Share Posted December 8, 2010 great, thanks so is there a different way to do this maybe a for loop I dunno kinda lost at this point? Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 8, 2010 Author Share Posted December 8, 2010 "<tr><th>Player</th><th>Experience</th></tr><tr align=\"center\"><td>john doe</td><td>894</td></tr>" this is drawing out exactly what I want.. problem is I want the next record to just add itself to the table in DESC order of experience right now it's drawing that out for each record as you said it will do this during a while loop Quote Link to comment Share on other sites More sharing options...
rondog Posted December 8, 2010 Share Posted December 8, 2010 echo "<table border=\"1\" align=\"left\"><tr><th>Player</th><th>Experience</th></tr>"; while ($row = mysql_fetch_assoc($result)) { echo "<tr align=\"center\"><td>john doe</td><td>894</td></tr>"; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted December 8, 2010 Author Share Posted December 8, 2010 ooohh k, figured it out draw the table and set the TH prior to entering the loop Quote Link to comment 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.