Counting records with PHP/MySQL

  • tommya
  • Graduate
  • Graduate
  • tommya
  • Posts: 221
  • Loc: United Kingdom

Post 3+ Months Ago

if I have a column in a MySQL database with the name

"Closed"

and properties of

ENUM('Y','N')

then how do I use PHP to count how many records are set, for example, to Y

Cheers
Tommy
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • tarka
  • Born
  • Born
  • tarka
  • Posts: 1
  • Loc: london

Post 3+ Months Ago

this should do it. php doesn't actually do the count, mysql does it embedded inside php.

Code: [ Select ]
$sql = ("SELECT COUNT(*) Closed
FROM your_table
WHERE your_table.Closed = 'Y'
$result = mysql_query($sql);
        
// print the count result to your browser
$count = mysql_result($result,0,0);
print ($count);
  1. $sql = ("SELECT COUNT(*) Closed
  2. FROM your_table
  3. WHERE your_table.Closed = 'Y'
  4. $result = mysql_query($sql);
  5.         
  6. // print the count result to your browser
  7. $count = mysql_result($result,0,0);
  8. print ($count);
  • tommya
  • Graduate
  • Graduate
  • tommya
  • Posts: 221
  • Loc: United Kingdom

Post 3+ Months Ago

what is the "0,0" bit for, when setting the $count variable?
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

Quote:
mixed mysql_result ( resource result, int row [, mixed field])


mysql_result() returns the contents of one cell from a MySQL result set. The field argument can be the field's offset, or the field's name, or the field's table dot field name (tablename.fieldname). If the column name has been aliased ('select foo as bar from...'), use the alias instead of the column name.

http://us2.php.net/mysql_result

looks the the first zero is the row and the second zero is the column offset. Since the query returns only one column, it is column 0.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.