Jump to content

Archived

This topic is now archived and is closed to further replies.

Fyorl

while loop processing

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I think I see what you mean. But how would I get the results from that query into 3 seperate unique variables?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.