0

i'm trying to show the count value from a column in my table.

my table is called ptb_forum, and it looks like this:

id  |  from_user_id  |  content  |  title  |  read_forum  | deleted

1           3            hello      test           20          0
2           6            bored      hello          40          0

i am basically trying to get the number in read_forum (so 20 or 40 or whatever) to echo out in php by using this mysql code:

function check_new_forum2() {
            global $connection;
            global $_SESSION;
            $query = "SELECT COUNT(id) read_forum FROM ptb_forum WHERE ptb_forum.read_forum=ptb_forum.from_user_id AND ptb_forum.deleted='0' 
        ";
            $check_new_forum_set2 = mysql_query($query, $connection);
            confirm_query($check_new_forum_set2);
            return $check_new_forum_set2;       
        }

and echoing out the result using this php:

$check_new_forum_set2 = check_new_forum2();
while ($newf = mysql_fetch_array($check_new_forum_set2)) {

    echo "<div class=\"forum_comments\">". $newf['COUNT(id)'] ."&nbsp;Views</div>";
 } ?>

at the moment nothing is being echoed, and i dont know where im going wrong, im new to mysql and am only just learning count, can someone please show me where im going wrong. thanks

4
  • mysql_* are depricated please dont rely on them Commented Mar 14, 2013 at 1:24
  • 1
    SELECT COUNT(id) AS c and use $newf['c'] Commented Mar 14, 2013 at 1:25
  • Are you checking for MySQL errors? I think I see at least one syntax error, you need a comma at least: COUNT(id), read_forum Commented Mar 14, 2013 at 1:26
  • 1
    Ah, without the comma you're setting COUNT(id) AS read_forum Commented Mar 14, 2013 at 1:29

3 Answers 3

3

Because you haven't got a comma between SELECT COUNT(id) and read_forum your sql is doing this

SELECT COUNT(id) AS read_forum

So you need to use $newf['read_forum'] or do this if read_forum is a column:

SELECT COUNT(id) AS c, read_forum 

and use

$newf['c']
Sign up to request clarification or add additional context in comments.

1 Comment

I was not sure if read_forum was a column either, but this was going to be my answer.
0

You should check $query if it returns false. If so there is an error in your SQL.

Perhaps the select statement should have read:

SELECT COUNT(id), read_forum FROM ptb_forum
WHERE ptb_forum.read_forum=ptb_forum.from_user_id
AND ptb_forum.deleted=0

if ptb_forum.deleted is a number and not a string

Comments

-1

Use $newf['read_forum'] instead of $newf ['count(id)']
I don't think there is any use of count(Id) in the query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.