Jump to content

[SOLVED] Why is my array size 0??


mike12255

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.