mike12255 Posted February 18, 2009 Share Posted February 18, 2009 I sent a query and im tryingto find out the amount of posts inside a topic and i got the followng code to do so, but it returns the array size 0, anyone know how to fix?? <?php include ("connect.php"); $id = 22; $area = 'math'; /* Err... Let p be the number of posts, let m be the number of posts per page and let y be the number of pages. y=p/m*/ $postquery = "Select * from forumtutorial_posts WHERE parentid = $id AND area = '$area'"; $findamount = mysql_query($postquery) or die (mysql_error()); $array = array(); while ($row = mysql_fetch_assoc($findamount)) { $array = $row['posts']; } $testt = sizeof($array); echo $testt ?> Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/ Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 Yes I do. It is because you are not doing the assignment you think you are. What you want to do is $array[] = $row['posts']; Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764785 Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 Well one other potential issue: your query might not be working. You should test that independently --- print_r() is your friend when debugging. Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764786 Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 Yes I do. It is because you are not doing the assignment you think you are. What you want to do is $array[] = $row['posts']; He declared the array earlier: $array = array(); Do you think he still has to do this? $array[] Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764790 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 Yes I do. It is because you are not doing the assignment you think you are. What you want to do is $array[] = $row['posts']; He declared the array earlier: $array = array(); Do you think he still has to do this? $array[] Yes. Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764792 Share on other sites More sharing options...
haku Posted February 18, 2009 Share Posted February 18, 2009 What he said. If you don't add the square brackets, it just turns it into a variable, and keeps overwriting that variable with each iteration of the loop. Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764793 Share on other sites More sharing options...
mike12255 Posted February 18, 2009 Author Share Posted February 18, 2009 yeah i had to do array[], and it worked. Thanks Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764796 Share on other sites More sharing options...
trq Posted February 18, 2009 Share Posted February 18, 2009 In fact an entirely better way to get a count would be to do it within your query. <?php include ("connect.php"); $id = 22; $area = 'math'; $sql = "SELECT COUNT(id) FROM forumtutorial_posts WHERE parentid = $id AND area = '$area'"; if ($result = mysql_query($postquery)) { if (mysql_num_rows($result)) { echo "There are " . mysql_result($result, 0) . " posts"; } } ?> Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764797 Share on other sites More sharing options...
gizmola Posted February 18, 2009 Share Posted February 18, 2009 In fact an entirely better way to get a count would be to do it within your query. include ("connect.php"); $id = 22; $area = 'math'; $sql = "SELECT COUNT(id) FROM forumtutorial_posts WHERE parentid = $id AND area = '$area'"; if ($result = mysql_query($postquery)) { if (mysql_num_rows($result)) { echo "There are " . mysql_result($result, 0) . " posts"; } } ?> Thorpe +1. I agree entirely -- if all you want is the count, then just do the count in the db. Only do the loop if you actually need the individual post row data for something else on the page. Link to comment https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/#findComment-764812 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.