Lamez Posted July 24, 2008 Share Posted July 24, 2008 alright similar to my last post, but different problem. My script shows who is online, their account name, and their character name. But when there are more than one account logged in, it only shows one person online, when there are more than one. I have no idea what so ever why this is doing this, perhaps you guys do. code: <?php define(ACC_SERVER, "localhost"); define(ACC_USER, "user"); define(ACC_PASS, "pass"); define(ACC_NAME, "name"); $connection_a = mysql_connect(ACC_SERVER, ACC_USER, ACC_PASS); $sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error()); $char = mysql_fetch_array($sql); $id = $char['acct']; $name = $char['name']; $sql = mysql_query("SELECT * FROM ".ACC_NAME.".accounts WHERE `acct` = '$id'")or die(mysql_error()); $on_num = mysql_num_rows($sql); ?> <font face="Arial, Helvetica, sans-serif"> <h1 align="center">People Online</h1> <table width="50%" border="0" bgcolor="#666666" align="center"> <tr> <td> <?php if($on_num == (0)){ echo "There are no players online."; } while($char = mysql_fetch_array($sql)){ echo $char['login']; echo "(".$name.")"; echo " "; } ?></td> </tr> <tr> <td><strong>Total Online</strong>: <font color="#FFFFFF"><?php echo $on_num; ?></font></td> </tr> </table> </font> Quote Link to comment Share on other sites More sharing options...
Lamez Posted July 24, 2008 Author Share Posted July 24, 2008 also the total number, shows one as well. Quote Link to comment Share on other sites More sharing options...
Lamez Posted July 24, 2008 Author Share Posted July 24, 2008 I cleaned up the code a bit. Nothing Different. <?php define(ACC_SERVER, "localhost"); define(ACC_USER, "usr"); define(ACC_PASS, "pass"); define(ACC_NAME, "name"); mysql_connect(ACC_SERVER, ACC_USER, ACC_PASS); $sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error()); $char = mysql_fetch_array($sql); $id = $char['acct']; $char_name = $char['name']; $sql_r = mysql_query("SELECT * FROM ".ACC_NAME.".accounts WHERE `acct` = '$id'")or die(mysql_error()); $on_num = mysql_num_rows($sql_r); ?> <font face="Arial, Helvetica, sans-serif"> <h1 align="center">People Online</h1> <table width="50%" border="0" bgcolor="#666666" align="center"> <tr> <td> <?php if($on_num == (0)){ echo "There are no players online."; } while($acc = mysql_fetch_array($sql_r)){ $acc_name = $acc['login']; echo $acc_name."(".$char_name.") "; } ?></td> </tr> <tr> <td><strong>Total Online</strong>: <font color="#FFFFFF"><?php echo $on_num; ?></font></td> </tr> </table> </font> Quote Link to comment Share on other sites More sharing options...
trq Posted July 24, 2008 Share Posted July 24, 2008 If your only getting one result and the total number also states one then your query is only finding one matching result. Quote Link to comment Share on other sites More sharing options...
sader Posted July 24, 2008 Share Posted July 24, 2008 try to clean upper <?php ?> part a bit <?php define(ACC_SERVER, "localhost"); define(ACC_USER, "usr"); define(ACC_PASS, "pass"); define(ACC_NAME, "name"); mysql_connect(ACC_SERVER, ACC_USER, ACC_PASS); $sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error()); $on_num = mysql_num_rows($sql); ?> Quote Link to comment Share on other sites More sharing options...
dezkit Posted July 24, 2008 Share Posted July 24, 2008 <?php if($on_num == (0)){ echo "There are no players online."; } while($acc = mysql_fetch_array($sql_r)){ $acc_name = $acc['login']; echo $acc_name."(".$char_name.") "; } ?> the whole code is messed up Quote Link to comment Share on other sites More sharing options...
Lamez Posted July 24, 2008 Author Share Posted July 24, 2008 well dezkit, what is messed up about it? -Thanks Guys! Quote Link to comment Share on other sites More sharing options...
sader Posted July 24, 2008 Share Posted July 24, 2008 Did you solve it? BTW code is realy messy a bit Quote Link to comment Share on other sites More sharing options...
Lamez Posted July 24, 2008 Author Share Posted July 24, 2008 ya I am working on cleaning up my code, and no I have not solved it. I think I might need to make another loop. Quote Link to comment Share on other sites More sharing options...
Lamez Posted July 24, 2008 Author Share Posted July 24, 2008 ok so what I am trying to do is see who is online, but I have a problem. The name of the account is in one table, and to tell if they are online is in a different table accounts- acct(id) login(acc name) characters id char_name online so I wanna take the the id, and find the account name. Then I wanna find out which char is online. So I think I might have to make 2 loops Quote Link to comment Share on other sites More sharing options...
sader Posted July 24, 2008 Share Posted July 24, 2008 I think it would be much easyer just add one more column into accounts table accounts- id(auto_increment) login(char) logged(tinyint) when user logs in u upadte table and set logged to 1 when user logs out u set it to 0 Quote Link to comment Share on other sites More sharing options...
Laogeodritt Posted July 24, 2008 Share Posted July 24, 2008 Just a note, I'm trying to figure everything out based on what you say works in your code and the PHP manual, since I haven't started working with MySQL in PHP. By the looks of it, to iterate through ALL the rows, you'd have to loop for the first mysql_fetch_array() call. But consider that $id and $char_name would be overwritten each time — you'll have to nest the second loop into the first one, I think. You're also checking for no players online but still going through with outputting the users afterwards. I'd do something like this. <?php define(ACC_SERVER, "localhost"); define(ACC_USER, "usr"); define(ACC_PASS, "pass"); define(ACC_NAME, "name"); mysql_connect(ACC_SERVER, ACC_USER, ACC_PASS); $sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error()); $on_num = mysql_num_rows($sql_r); if($on_num == (0)){ $on_list = "There are no players online."; } else { while($char = mysql_fetch_array($sql)) { $id = $char['acct']; $char_name = $char['name']; $sql_r = mysql_query("SELECT * FROM ".ACC_NAME.".accounts WHERE `acct` = '$id'")or die(mysql_error()); while($acc = mysql_fetch_array($sql_r)){ $acc_name = $acc['login']; $on_list .= $acc_name." (".$char_name.") "; } } }?><font face="Arial, Helvetica, sans-serif"> <h1 align="center">People Online</h1> <table width="50%" border="0" bgcolor="#666666" align="center"> <tr> <td><?php echo $on_list; ?></td> </tr> <tr> <td><strong>Total Online</strong>: <font color="#FFFFFF"><?php echo $on_num; ?></font></td> </tr> </table> </font> Also, your HTML code is invalid for HTML 4.01. You can't use block elements (h1, table, etc.) in an inline element (in this case, font); if you want the whole page to use those fonts, use CSS on <body> tag; otherwise, use a <div>, which is a block element, and CSS. @sader: I assume he wants to keep the data separate because char_name != login, and perhaps that a given user account can have multiple characters (though probably only one "active" at a time if that's the case). Quote Link to comment Share on other sites More sharing options...
Lamez Posted July 24, 2008 Author Share Posted July 24, 2008 Gosh thank you so much. I am too lazy to mess with CSS, lol. This is for my use any ways. I did not setup the database, but Account name is a login, and they also have characters, they can have more than one character. Thank you so much! 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.