Parse & JS: Counting objects

This entry is part 21 of 25 in the series Parse & Javascript

In SQL, we always use select count to get the number of records in a table.

For Parse, we can use the query count function.

<!doctype html>
    <title>My Parse App</title>
    <script type='text/javascript' src=''></script>
    <script type='text/javascript' src=''></script>


    <div style='display:none' class='error'>
        <h1>Movie not found</h1>

    <div style='display:none' class='success'>
        <h1>Movie Found</h1>

    Parse.initialize('5d3sro43zBGj8AMYqWND5bFYMzAan7f5P5euZm7M', 'hlRZZqAylA7Fmah4Kk6ma8WK7QCT4cHrxjafnnkI');
    var MoviObject = Parse.Object.extend('MoviObject');
    var moviObject = new MoviObject();

    var query = new Parse.Query(MoviObject);

    query.equalTo('releaseYear', 2013);

        success: function(count) {
            // The count request succeeded. Show the count
            document.write("No of movies released in the year 2013:  " + count);
        error: function(error) {
            // The request failed

Try The Code

PHP MySQL: Select Count Using fetch_assoc()

This entry is part 10 of 17 in the series PHP & MySQL

If we are to use

select count(*) from artist

in the query statement, we can use the fetch_assoc() method provided by PHP.

The syntax is:

array mysqli_result::fetch_assoc ( void )

select count(*) from artist

will only return a row and a column.  For associative array, we would need to have a column name.

We can assign a column name cnt as

select count(*) as cnt from artist

The PHP code would be as below:

$mysqli = new mysqli("localhost", "root", "", "allartist");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());

$query = "select count(*) as cnt from artist ";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */

    $row = $result->fetch_assoc();
    printf("Number of records:  %s", $row["cnt"]);

    /* free result set */

/* close connection */

Here the screenshot after running the script above:


PHP MySQL: Select count

This entry is part 9 of 17 in the series PHP & MySQL

When it comes to querying a database, we always like to use select count to get the number of records in the table.

select count(*) from artist

However, let’s look at the code below.

To get the select count, we first pass the query string into $mysqli->query().

$mysqli = new mysqli("localhost", "root", "", "allartist");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());

if ($result = $mysqli->query("SELECT * FROM artist")) {

    /* determine number of rows result set */
    $row_cnt = $result->num_rows;

    printf("Result set has %d rows.", $row_cnt);

    /* close result set */

/* close connection */

If connection to the DB is successful, we should get the following screenshot displayed on the browser.

select count

We are using

select * from artist

and yet we are able to get the number of records in the table.  This is because we are using OOP way of programming.

$result = $mysqli->query("SELECT * FROM artist")

In OOP, a variable is an object.  After executing the above statement, the relevant methods and properties associated with the object could be used.

In this case we have used

$row_cnt = $result->num_rows;

to retrieve the number of records in the table artist.

SQL: Functions

This entry is part 20 of 20 in the series SQL Tutorial

There are a slew of mathematical functions in SQL such as avg(), max() and sum.  We have already made use of count() which will return the total number of records.

Let’s look at some common functions in SQL.

 id int auto_increment primary key, 
 artiste varchar(30), 
 album varchar(50),
 price decimal(3, 2) 

(artiste, album, price)
('Rod Stewart', 'Maggie May', 8.90 ),
('New Order', 'Substance', 7.50),
('Nirvana', 'Smell Like Teen Spirit', 9.80);

SQL Fiddle

select sum(price) from artist
select avg(price) from artist
select max(price) from artist
select min(price) from artist
select count(price) from artist