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
https://forums.phpfreaks.com/topic/145673-solved-why-is-my-array-size-0/
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";
  }
}

?>

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.

Archived

This topic is now archived and is closed to further replies.

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