RSS

›Php and Mysql Count() Function

This entry was posted on Nov 14 2010
Final Image

Count all elements in an array, or properties in an object, This is particularly useful for counting all the elements of a multidimensional array. The default value for mode is 0. count() does not detect infinite recursion.

Php Count() Function Syntax:

 count(array,mode) 

array: Required. Specifies an array.

mode: Optional. Specifies the mode of the function.

Php Count() Function Examples:

Example 1

<?php
$fruits = array("Orange", "Apple", "Leech", "Mango");
$count = count($fruits);
echo "There are $count type of fruits";
?>

Output:

There are 4 type of fruits

Example 2

<?php
$value= array(1,2,3,4,5);
$count = count($value);
echo "There are $count Values in the Array";?>

Output:

There are 5 Values in the Array

Example 3

<?php
$n[0] = 'column one';
$n[1] = 'column two';
$n[2] = 'column three';
$result = count($n);
echo "There are $result Columns";
?>

Output:

There are 3 Columns

MySql Count() Fucntion

The MySql Count() function returns the number of rows in a query. The MySql Count() function will only count those records in which the field in the brackets is NOT NULL.

Syntax

SELECT COUNT(expression)
FROM tables
WHERE predicates;

Examples

Example 1

if you have the following table in your MySql Datbase, called students:

StudentID Student_Name Father_Name
#1 Michal Josh Jones
#2 Gautam Gupta Norman Yung
#3 Kieran Smith Kerem Erkan
<?php
/// Counting number of students in the students table
$query = "SELECT COUNT(Student_Name) FROM students";
$result = @mysql_query($query);
while($row = @mysql_fetch_array($result)){
	echo "There are ". $row['COUNT(Student_Name)']. "Students in the Database";
}?>

Output:

There are 3 Students in the Database

Example 2

While the result for the next query will only return 1, since there is only one row in the students table where the “Father_name” field is NOT NULL.

StudentID Student_Name Father_Name
#1 Michal Josh Jones
#2 Gautam Gupta
#3 Kieran Smith
<?php
/// Counting students with father name in the students table
$query = "SELECT COUNT(Father_Name) FROM students";
$result = @mysql_query($query);
while($row = @mysql_fetch_array($result)){
	echo "There is ". $row['COUNT(Student_Name)']. "Student with Father Name";
}?>

Output:

There is 1 Student with Father Name

Counting Multiple MySql Database Records

The COUNT function is an aggregate function that simply counts all the items that are in a group. One use of COUNT might be to find out how many Students are in the each “City”

Examples:

Example 1

StudentID Student_Name Father_Name City
#1 Shah Khan Muhamad Ali England
#2 Abdul Allah Ahmad Hussain Canada
#3 Mujahid Saif u Allah England
#4 Maaz Khan Jaleel Khan USA
#5 Shabeer Ahmad Shah England
#6 Farooq Gul Haji Canada
#7 Sanobar Musafar Khan USA
#8 Usama Wali Khan Canada
<?php
$query = "SELECT City, COUNT(Student_Name) FROM students GROUP BY City";
$result = @mysql_query($query);
while($row = @mysql_fetch_array($result)){
	echo "There are ". $row['COUNT(Student_Name)'] ." Students from ". $row['City'] ."";
	echo "<br />";
}
?>

Output:

There are 3 Students from  England.
There are 3 Students from  Canada.
There are 2 Students from  USA.
About admin
View all post written by admin
Hello, My name is Shah Hussain I am a Freelance Php Developer, i wrote free php tutorials
This author written about (20) articles


5 Responses to “Php and Mysql Count() Function”

  1. To be exact, COUNT() explained in the second part of the “tutorial” isn’t a PHP function(pointing to the big title of this tutorial). It’s a SQL function.

    Secondly, if you’re writing a tutorial, please DO NOT leave out important things like:
    How did this:
    “Select COUNT(StudentID) from students;”
    get to your output, which is:
    There are 3 Students in the Database

    The actual output of that SQL given is 3 not “There are 3 Students in the Database”

    Also, reading white text from white background isn’t working very well.


  2. Hello,
    Thanks for your help, i have fixed all the mistake, please re check the post :)


  3. “$result = @mysql_query($query) or die(@mysql_error());”

    I don’t actually see why you should suppress errors here while you’re handling them yourself.

    “or die(mysq_error())” itself means that IF THE query FAILS then DIE and print MYSQL_ERROR.

    Even if you want to suppress mysql_query then I see no reasons at all suppressing mysql_error function.

    Otherwise seems OK


  4. alright, everything is fixed now, thanks for your help…


  5. Wonder Example of Count ZTutorial
    Thank sir


Post a Comment