Darkmatter5 Posted July 1, 2008 Share Posted July 1, 2008 Looking at this code <?php include 'library/config.inc.php'; require('C:\wamp\www\library\opendb.php'); $pagedb = "members"; $result = mysql_query("SELECT mem_username FROM " .$dbname. "." .$pagedb. " WHERE logged_in = '1'") or die(mysql_error()); $count = count(mysql_fetch_array($result)); echo "There are " .$count. " results!<br>"; while($row = mysql_fetch_array($result)) { echo $row['mem_username']. "<br>"; } require('C:\wamp\www\library\closedb.php'); ?> Say there are 2 users in the table (john.smith and jane.smith), both with the logged_in field set to 1. Why does it only output this example: There are 2 results! john.smith Link to comment https://forums.phpfreaks.com/topic/112822-strange-result-from-queries/ Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Because you call mysql_fetch_array once to get the first row. I don't know why the hell you do that. =X Change this line: $count = count(mysql_fetch_array($result)); To: $count = mysql_num_rows($result); Then it'll work. Link to comment https://forums.phpfreaks.com/topic/112822-strange-result-from-queries/#findComment-579472 Share on other sites More sharing options...
Darkmatter5 Posted July 1, 2008 Author Share Posted July 1, 2008 Excellent thanks! Now how can I implement printing out all the users that have the logged_in field set to 1 with a "," in between each one. The code I have does it, but it also does it to the last user name as well. And grammatically that's wrong. So how can I do this without putting a "," after the last user? <?php include 'library/config.inc.php'; require('C:\wamp\www\library\opendb.php'); $pagedb = "members"; $query = mysql_query("SELECT mem_username FROM " .$dbname. "." .$pagedb. " WHERE logged_in = '1'") or die(mysql_error()); while($row = mysql_fetch_array($query)) { echo $row['mem_username']. ", "; } require('C:\wamp\www\library\closedb.php'); ?> I thought about using the mysql_num_rows code you last told me about, but I can't think of how to use it. Link to comment https://forums.phpfreaks.com/topic/112822-strange-result-from-queries/#findComment-579505 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Change: echo $row['mem_username']. ", "; To: $names[] = $row['mem_username']; Then, after the loop but before the require, do: echo implode(",", $names); Link to comment https://forums.phpfreaks.com/topic/112822-strange-result-from-queries/#findComment-579508 Share on other sites More sharing options...
Darkmatter5 Posted July 1, 2008 Author Share Posted July 1, 2008 amazing!! *sigh* thanks again! Link to comment https://forums.phpfreaks.com/topic/112822-strange-result-from-queries/#findComment-579530 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Any time. Link to comment https://forums.phpfreaks.com/topic/112822-strange-result-from-queries/#findComment-579534 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.