Reputation: 1
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)'] ." 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
Upvotes: 0
Views: 716
Reputation:
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']
Upvotes: 3
Reputation: 28741
Use $newf['read_forum']
instead of $newf ['count(id)']
I don't think there is any use of count(Id) in the query
Upvotes: -1
Reputation: 59997
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
Upvotes: 0