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


 Subscribe in a reader

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

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 10th, 2008, 21:27
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 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
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 10th, 2008, 22:13
Reputable Member
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 10th, 2008, 22:20
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 11th, 2008, 17:32
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 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.
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 11th, 2008, 17:46
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,794
Thanks: 0
Thanked 17 Times in 17 Posts
Re: Count something in MySQL Table?

It will still work as long as you have a variable called $articleid.
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 11th, 2008, 17:46
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 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
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
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 15:32.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization 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