Forum Count help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Forum Count help

Post by Smackie »

I created a forum and I am having problem with the counting of each topic and replies..

currently they count all the topics and posts i need it to only count the different id's like right now I got to posts on there with 5 topics they are both saying 5 topics in each there should be 2 in forumid 1 and 3 in forumid 2

here is the script that counts the forumid

Code: Select all

$query = mysql_query("SELECT forumid FROM forum_question");
$topics = mysql_num_rows($query);
can someone help me please
Thank you
Smackie
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

you need to count by id

Post by yacahuma »

you need to count by id

Code: Select all

$query = mysql_query('SELECT count(key here) FROM forum_question where forumid=' .  $forumid);//or what ever is your key
just get the count returned by this
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

well that didn't work and so I went looking around and found this (i edit to my setting i need)


Code: Select all

$get_num_posts = "select count(forumid) from forum_question where postid = $postid";
$get_num_posts_res = mysql_query($get_num_posts) or die(mysql_error());
$num_posts = mysql_result($get_num_posts_res,0,'count(forumid)'); 

echo $num_posts;
but for some reason i get this


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Smackie wrote:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Are you sure that $postid is set?
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

what you mean by set? you mean in database yes its in database
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

I think he means, has $postid been set previously in your code.

Ie.

Code: Select all

$postid = $_POST['postid'];

or $postid = 1;
something like that, above your SQL statement.

If not, your statement...

Code: Select all

$get_num_posts = "select count(forumid) from forum_question where postid = $postid";
really means...

Code: Select all

$get_num_posts = "select count(forumid) from forum_question where postid = absolutely nothing (or 0)";
which obviously, would return, absolutely nothing.
Last edited by iknownothing on Sun Jul 29, 2007 11:18 pm, edited 1 time in total.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

oh no i have

Code: Select all

$id = $_POST['id'];
even when i put id = $id in there it still does the same
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

if you have used...

Code: Select all

$id = $_POST['id'];

instead of

$postid = $_POST['id'];
then $postid, is indeed not set, make sure you use the correct variable name within your SQL, otherwise it won't return accurate results, if any at all.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

like i said even if i used the set id (i even set the postid) and still get the same error
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

I just think im going have to recode it and see if i can figure out where it when wrong but thanks for the help if i need more ill be back..
Thank you
Smackie
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

oh, possibly surround $postid, or $id, whichever with single quotes, I only just noticed your error was near the ", presumably the one at the end...

ie.

Code: Select all

$get_num_posts = "select count(forumid) from forum_question where postid = '$postid'";
I beleive that PHP may think that, without the single quotes $postid is a string, not a variable
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

ok im back i figured out it but still not returning what i need its returning as both posts as 0 here is what i got now


Code: Select all

<?php 
$id = $_GET['id'];

	$sql = mysql_query("SELECT * FROM header ORDER BY `id`");
	while($rows = mysql_fetch_array($sql)){
	$id = $rows['id'];
	$a_id = $rows['id'];
	
	$datetime = date('D dS M Y, h:i a', strtotime($rows['datetime']));

$get_num_topics = "select count(forumid) from forum_question where id = $id";
$get_num_topics_res = mysql_query($get_num_topics) or die(mysql_error());
$topics = mysql_result($get_num_topics_res,0,'count(forumid)'); 

$get_num_posts = "select count(question_id) from forum_answer where a_id = $a_id";
$get_num_posts_res = mysql_query($get_num_posts) or die(mysql_error());
$posts = mysql_result($get_num_posts_res,0,'count(question_id)'); 
?>


its alot more lol :)
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

Can you please explain how you have set up your database, because I'm finding it very difficult to understand where your values are coming from, and why you need the WHERE statement in your SQL at all, considering you are trying to count all, not 1.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

header

id, header, description

forum question

id, forumid, topic, detail, userid, lastpost, datetime, view, reply

forum answer

question_id, a_id, userid, a_answer, a_datetime
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

iknownothing wrote:really means...

Code: Select all

$get_num_posts = "select count(forumid) from forum_question where postid = absolutely nothing (or 0)";
which obviously, would return, absolutely nothing.
Actually, what I was implying was that his query was ending awkwardly with an equals sign, which is why MySQL was saying that the error was an the end of the query.

Code: Select all

select count(forumid) from forum_question where postid =
See? If $postid was empty, there would be nothing after the equals sign.
Post Reply