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 = ''; Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/ 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144098 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144099 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144105 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) Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144107 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? Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144108 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? Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144121 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144130 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"; Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144132 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144152 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>"; Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144166 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>"; Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144181 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); ?> Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144205 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.. Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144208 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144211 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? Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144230 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. Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144231 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? Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144233 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144234 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>"; Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144235 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 Link to comment https://forums.phpfreaks.com/topic/220955-a-simple-select/#findComment-1144236 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.