Jump to content

while loop processing


Fyorl

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?

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.