[SOLVED] Count something in MySQL Table?

This is a discussion on "[SOLVED] Count something in MySQL Table?" within the PHP Forum section. This forum, and the thread "[SOLVED] Count something in MySQL Table? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 10th, 2008, 21:27
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
[SOLVED] Count something in MySQL Table?

Hi guys,

I'm building my own blog system to try and learn PHP. I have a comments table, where each comment has an ID, which relates to the ID of the article.

Example: Comments on Article No.1 have an ID of 1.

I want to count how many comments (IE, rows with ID=(ARTICLE ID)) so, if the article id=1, then I want to count the number of rows with the ID of 1.

How can I do this? Do you understand what I am saying?

Jack
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote

  #2 (permalink)  
Old Jan 10th, 2008, 22:13
SuperMember

SuperMember
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Count something in MySQL Table?

I'm no expert and someone will probably give you a better answer but something like this might work

PHP: Select all

$sql =  mysql_query("SELECT count(id) FROM article WHERE id = "1"") or die(mysql_error()); 
$results mysql_result($sql"0"); 
print(
$results); 
if it doesnt work at least it may give you some ideas.

Pat
Reply With Quote
  #3 (permalink)  
Old Jan 10th, 2008, 22:20
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,772
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Count something in MySQL Table?

Typo in that code.. this should work.
PHP: Select all


$sql 
=  mysql_query("SELECT count(id) FROM comments WHERE id = '1'") or die(mysql_error()); 
$results mysql_result($sql"0"); 
print(
$results); 
I'd like to see your database structure.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #4 (permalink)  
Old Jan 11th, 2008, 17:32
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Count something in MySQL Table?

From looking at the code that seems absolutely pefect for the job. If I change it to
PHP: Select all

WHERE id '$articleid' 

Will that still work?

I'll try it and get back to you. Thanks for the response.
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #5 (permalink)  
Old Jan 11th, 2008, 17:46
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,650
Thanks: 0
Thanked 8 Times in 8 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: Count something in MySQL Table?

It will still work as long as you have a variable called $articleid.
Reply With Quote
  #6 (permalink)  
Old Jan 11th, 2008, 17:46
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Count something in MySQL Table?

Ok thanks. SOLVED

(For now anyway, might return if it fails :P)

Thanks, as usual, for the fantastic help

Jack
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Post Count Not Going Up... TheLion Webforumz Cafe 1 Jan 16th, 2008 09:57
[SOLVED] Page count? unitedcraig JavaScript Forum 11 Nov 7th, 2007 07:07
Viewing the contents of a table in MySQL nemmea Databases 3 Sep 6th, 2007 22:01
MySQL UPDATE after use $uniqueID to find table row, etc jswebdev PHP Forum 1 Dec 6th, 2005 12:09


All times are GMT. The time now is 07:14.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43