How do I count the of records in database and display them in webpage?

OS info

  • Operating System : Mac OSX 17.7.0
  • Wappler Version : 1.2.0

how do I count the number of records in mysql and display them in webpage

You can count how many records are in a table by using the data formatters built into Wappler.

Steps:
Create a server connect action file
Right click steps, database actions, add database connection, and setup accordingly.
Right click your connection step, database actions, add database query, setup the query without the count, just query the data you want to be counted.
Save your action

In your Wappler page where you want to display the count, go the the App Structure panel, make sure you have already added App Connect,
click on App,
right click, choose Data, choose Server Connect,
click the component you just added
Select the new server action you just made

Now select where you want to display the info, or choose the container in App Structure and Insert Binding, in the panel, select the Server Connect component you just added, then arrow it out, arrow out data, and select the entire array, at the bottom of the panel you will see your selection looking like serverconnect1.data.query1, to the right of that click the magic want to find all the available formatters, select your array, right click and choose Collections, then count. As there are no Properties for Count, you can click Select to close the formatters. Then click select again to confirm the first popup. Save, and check if its working.

7 Likes

Great post Paul! A bit more text formatting if the steps, add numbers, few screenshots and we have a full guide here :slight_smile:

You can use markdown in the topic text btw

There are multiple ways to get the count from a database, depending on your needs.

method 1: If you don’t want to retrieve the records and only want to get the count, you can do that with a simple database query. In the query builder select the table, then add only the id column, select aggregate count and give it an alias.

method 2: If you get all the records in the query from the database, you can do the count in app connect using the data formatters. Just like Paul explains.

method 3: You can use a paged database query action to get only a limited set off your data, the data returned from this action also includes the total number of records, the number of pages and more.

5 Likes

In addition to what you all describe here, below is another approach if you want to count based on several filters with just using one query. For example let’s say that you want to count
how many people have declared interest for a service
and how many have actually payed for it.

1.Create your query just like @psweb describes
2.Add your query though AppConnect on your page
3.Create two different dataviews
3a. Use the filter option (for the dataview) to filter the first view
3b. Use the filter option (for the dataview) to filter the second view
4. For each of these dataviews bind the ‘items’ value just like this
{{data_view_count_only_interested.items}}

You can use screenshots below as reference. You will notice that 3 dataviews exist in the screenshot. You can create as many as you need.

3 Likes

This is a great thread. How can I show some content only if the record count is > 0?

Whoa, I must be getting good at this. I’ve already found the solution. Used the ‘Show’ dynamic attribute with an expression and found ‘hasItems’. Works perfectly.

2 Likes

Hello, I’m trying to get the min and max values of dates in a table, while it works correctly for counting (as explained on the thread), for max and min won’t wotk for dates (for numbers, min and max works).

Instead of retrieving me the maximun and minimun dates on a database, it shows me the word “infinity”