Jump to content

[SOLVED] count entries


dachshund

Recommended Posts

hi,

 

i'm just trying to count the amount of comments on an article but it's not working. can anyone see what's wrong? i have a connection etc etc.

 

<?php

$id=mysql_real_escape_string($_GET['id']);

echo mysql_query("SELECT COUNT(*) FROM comments WHERE articleid='$id'"); 

?>

 

 

Link to comment
Share on other sites

that's what i originally thought but for some reason that always returns '1'.

 

even when i just change it to

 

$commentresult=mysql_query("SELECT COUNT(*) FROM comments");

 

it returns 1. then i deleted all the entries in that table, and still it returns 1.

Link to comment
Share on other sites

that's because mysql_num_rows tells you how many rows were returned from your query.  Since you are only selecting count(*) you are only returning 1 row.  You need to use mysql_result or mysql_fetch_array or mysql_fetch_assoc or mysql_fetch_row.  You tried fetch_row and got "array" because fetch_row returns an array of all the columns of the row. 

 

In this particular instance, since you are only selecting count(*), you would echo $commentrow[0] after fetch_row

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.