Jump to content

Simple count() issue


V

Recommended Posts

Well, I'm sure it's cake for you.  :P

 

I'm just trying to display the total number of posts under a category.  But for example instead of 4 I get 1111. My code is,

 

////////////////Category Query


			$category_table = mysql_query("SELECT * FROM categories", $connection);
			if (!$category_table) {
				die("Database query failed: " .mysql_error());
			}


			while ($row = mysql_fetch_array($category_table)) { 


			$cat_id=$row['car_id'];
			$cat_name=$row['cat_name'];


				echo "<a href=\"single_category.php?cat=" . urlencode($cat_id) . "\">
				$cat_name</a><br />";	


////////////////Total Posts Query					


			$total_posts = mysql_query("SELECT post_title FROM posts WHERE cat_id = {$row["cat_id"]}", $connection);
			if (!$total_posts) {
				die("Database query failed: " .mysql_error());
			}


			while ($row = mysql_fetch_array($total_posts)) { 

			$posts_nr = count($row['post_title']);	

			echo $posts_nr;	

			}//total_posts loop




			}//topics

 

BTW, I'm sorry for posting so many questions these past 2 days.. PHP is fun :)

Link to comment
Share on other sites

$total_posts = mysql_query("SELECT COUNT(post_title) as total_posts FROM posts WHERE cat_id = {$row["cat_id"]}", $connection);
$row = mysql_fetch_assoc($total_posts);
echo $row['total_posts'];

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.