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
Post
by Smackie » Sat Jul 28, 2007 3:14 pm
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
yacahuma
Forum Regular
Posts: 870 Joined: Sun Jul 01, 2007 7:11 am
Post
by yacahuma » Sat Jul 28, 2007 4:22 pm
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 » Sun Jul 29, 2007 10:51 pm
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
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Sun Jul 29, 2007 11:06 pm
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 » Sun Jul 29, 2007 11:08 pm
what you mean by set? you mean in database yes its in database
iknownothing
Forum Contributor
Posts: 337 Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia
Post
by iknownothing » Sun Jul 29, 2007 11:15 pm
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 » Sun Jul 29, 2007 11:17 pm
oh no i have
even when i put id = $id in there it still does the same
iknownothing
Forum Contributor
Posts: 337 Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia
Post
by iknownothing » Sun Jul 29, 2007 11:20 pm
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 » Sun Jul 29, 2007 11:25 pm
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 » Sun Jul 29, 2007 11:27 pm
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
iknownothing
Forum Contributor
Posts: 337 Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia
Post
by iknownothing » Sun Jul 29, 2007 11:29 pm
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 » Sun Jul 29, 2007 11:50 pm
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
iknownothing
Forum Contributor
Posts: 337 Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia
Post
by iknownothing » Mon Jul 30, 2007 12:08 am
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 » Mon Jul 30, 2007 12:14 am
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
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Mon Jul 30, 2007 7:13 am
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.