Fyorl Posted September 29, 2003 Share Posted September 29, 2003 I\'m having trouble with my while loop. My code is setup as follows but it only returns 1 user when I know it should return 2: [php:1:1dfc5c6460]<?php $players_sql = mysql_query(\"SELECT username FROM sh_player WHERE dead=\'0\'\"); $players = mysql_fetch_array($players_sql); $num_players = mysql_num_rows($players_sql); $class_sql = mysql_query(\"SELECT class FROM sh_player WHERE dead=\'0\'\"); $class = mysql_fetch_array($class_sql); $level_sql = mysql_query(\"SELECT level FROM sh_player WHERE dead=\'0\'\"); $level = mysql_fetch_array($level_sql); /*print \"$players[0]\"; print \"$players[1]\";*/ echo \"<table><tr><td><b><font size=\"2\" face=\"verdana\" color=\"#6699CC\">Name</td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Class</td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Level</b></td></tr>\"; $n = 0; while ($n < $num_players) { echo \"<tr><td><font size=\"2\" face=\"verdana\" color=\"#6699CC\">$players[$n]</td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\">$class[$n]</td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\">$level[$n]</td></tr><td><tr>\"; $n++; } ?>[/php:1:1dfc5c6460] Is there something wrong with the way I\'m using the mysql_fetch_array function? Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/ Share on other sites More sharing options...
shivabharat Posted September 29, 2003 Share Posted September 29, 2003 The code looks fine but tell me \"$num_players \" whats the value returned here? Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3674 Share on other sites More sharing options...
Fyorl Posted September 29, 2003 Author Share Posted September 29, 2003 It returns 2 Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3678 Share on other sites More sharing options...
metalblend Posted September 29, 2003 Share Posted September 29, 2003 Actually there are a few things wrong.. try this: <?php $query = mysql_query("SELECT username,class,level FROM sh_player WHERE dead=\'0\'"); $total = mysql_num_rows($query); echo \'<table><tr><td><b><font size="2" face="verdana" color="#6699CC">Name</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC"><b>Class</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC"><b>Level</b></font></td></tr>\'; while($row = mysql_fetch_array($query,MYSQL_ASSOC) { echo \'<tr><td><font size="2" face="verdana" color="#6699CC">\' . $row[\'username\'] . \'</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'class\'] . \'</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'level\'] . \'</font></td></tr>\'; } echo \'</table>\'; ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3680 Share on other sites More sharing options...
Fyorl Posted September 29, 2003 Author Share Posted September 29, 2003 No, it\'s OK. I did a bit of research and used the mysql_result() function: [php:1:50f9efcd4d]<?php $players_sql = mysql_query(\"SELECT username FROM sh_player WHERE dead=\'0\'\"); $num_players = mysql_num_rows($players_sql); $class_sql = mysql_query(\"SELECT class FROM sh_player WHERE dead=\'0\'\"); $level_sql = mysql_query(\"SELECT level FROM sh_player WHERE dead=\'0\'\"); echo \"<table><tr><td><b><font size=\"2\" face=\"verdana\" color=\"#6699CC\">Name</td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Class</td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Level</b></td></tr>\"; $n = 0; while ($n < $num_players) { $players = mysql_result($players_sql, $n, \'username\'); $class = mysql_result($class_sql, $n, \'class\'); $level = mysql_result($level_sql, $n, \'level\'); echo \"<tr><td><font size=\"2\" face=\"verdana\" color=\"#6699CC\"> <a href=\"view.php?username=$players\">$players</a></td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\">$class</td><td> <font size=\"2\" face=\"verdana\" color=\"#6699CC\">$level</td></tr><td><tr>\"; ++$n; } ?>[/php:1:50f9efcd4d] Thanks for your time though. Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3682 Share on other sites More sharing options...
metalblend Posted September 29, 2003 Share Posted September 29, 2003 Why must you use 3 queries where 1 would generate the same result? Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3684 Share on other sites More sharing options...
Fyorl Posted September 29, 2003 Author Share Posted September 29, 2003 huh? Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3686 Share on other sites More sharing options...
metalblend Posted September 29, 2003 Share Posted September 29, 2003 You are using 3 queries: SELECT username FROM sh_player WHERE dead=\'0\' SELECT class FROM sh_player WHERE dead=\'0\' SELECT level FROM sh_player WHERE dead=\'0\' A quicker method, for both the script and the server, is to use 1 query to return all of the results: SELECT username,class,level FROM sh_player WHERE dead=\'0\' then loop through the results. You\'ve made it harder than it needs to be.. look into that. Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3688 Share on other sites More sharing options...
pauper_i Posted September 29, 2003 Share Posted September 29, 2003 take a look at the code implications: Metalblend: $query = mysql_query(\\\"SELECT username,class,level FROM sh_player WHERE dead=\'0\'\\\"); Your version: $players_sql = mysql_query(\\\"SELECT username FROM sh_player WHERE dead=\'0\'\\\"); $class_sql = mysql_query(\\\"SELECT class FROM sh_player WHERE dead=\'0\'\\\"); $level_sql = mysql_query(\\\"SELECT level FROM sh_player WHERE dead=\'0\'\\\"); Why would you go to the well three times when you can do the job in one. In other words, your server is going to work its butt off trying to keep up with your extra queries when it doesn\'t need to since you could pull the same info all in one go and access it from a memory array! D Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3689 Share on other sites More sharing options...
Fyorl Posted September 30, 2003 Author Share Posted September 30, 2003 I think I see what you mean. But how would I get the results from that query into 3 seperate unique variables? Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3695 Share on other sites More sharing options...
metalblend Posted October 1, 2003 Share Posted October 1, 2003 Using the method I gave earlier: <?php $query = mysql_query("SELECT username,class,level FROM sh_player WHERE dead=\'0\'"); $total = mysql_num_rows($query); echo \'<table><tr><td><b><font size="2" face="verdana" color="#6699CC">Name</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC"><b>Class</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC"><b>Level</b></font></td></tr>\'; while($row = mysql_fetch_array($query,MYSQL_ASSOC) { echo \'<tr><td><font size="2" face="verdana" color="#6699CC">\' . $row[\'username\'] . \'</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'class\'] . \'</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'level\'] . \'</font></td></tr>\'; } echo \'</table>\'; ?> or using your mysql_result() method (which still results in more code): <?php $query = mysql_query("SELECT username,class,level FROM sh_player WHERE dead=\'0\'"); $total = mysql_num_rows($query); echo \'<table><tr><td><b><font size="2" face="verdana" color="#6699CC">Name</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC"><b>Class</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC"><b>Level</b></font></td></tr>\'; for($i=0;$i<$total;$i++) { $username = mysql_result($query,$i,\'username\'); $class = mysql_result($query,$i,\'class\'); $level = mysql_result($query,$i,\'level\'); echo \'<tr><td><font size="2" face="verdana" color="#6699CC">\' . $username . \'</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC">\' . $class . \'</font></td><td>\'; echo \'<font size="2" face="verdana" color="#6699CC">\' . $level . \'</font></td></tr>\'; } echo \'</table>\'; ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/1081-while-loop-processing/#findComment-3724 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.