close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Display database statistics without multiple recordsets

Thread began 7/23/2014 1:39 pm by Dennis | Last modified 8/12/2014 5:30 pm by Dennis | 888 views | 7 replies |

Dennis

Display database statistics without multiple recordsets

Hi,

I created a page to show database statistics from a single table. Example total records from dues paid column containing 2014. As I knew no other way, I used $totalRows from a recordset to get this information. This worked well. The problem is I keep getting new ideas of statistics to list so now I have many, many recordsets in the page, which I have to believe is not a good idea.

How else can this be done? Also since I will probably recreate the page, can this be done with the MySQLi Server Behaviors

Thanks,
Dennis

Sign in to reply to this post

Jason ByrnesWebAssist

you can use count() and sum() and other math functions in the SQL statement and add an alias, for example:

SELECT COUNT(idColumnNAme) as recordCount, SUM(priceColumn) as totalPrice FROM tableName

yes, this can be done in MySQLi recordsets also.

details on SUM and other aggregate functions can be found here:
http://www.tizag.com/mysqlTutorial/mysqlsum.php

Sign in to reply to this post

Dennis

Thanks Jason,

Things came up and I haven't had a chance to work with your suggestions yet, but I did want to thank you for them.

Dennis

Sign in to reply to this post

Dennis

Jason,

Thanks again for you suggestion, the COUNT function looks very useful. I wonder if you could help me further...

Not understanding the concept of recordsets, I never know how to follow/use tutorials or examples that refer to SELECT statements. Are they always used inside a recordset? Or can they also be coded into the page?

I have a Dynamic MySQLi connection setup "connDennis_i"

What code would I enter to use this connection?

And using the COUNT part of the example you show above, "SELECT COUNT(idColumnNAme) as recordCount FROM tableName"

What code would I enter to use this SELECT statement?

And how would I echo the result?

Thanks,
Dennis

Sign in to reply to this post

Jason ByrnesWebAssist

Yes, select statements are always used in recordsets.

to use the connection you created, you should create a recordsety that uses that connection, that will generate the code to use your connection.

in the recordset, go to advanced view, and enter the SQL statement:
SELECT COUNT(idColumnNAme) as recordCount FROM tableName


where idColumnNAme is the name of the ID column in your table and tableName is the name of the table.

to echo the result to the page, open the bindings panel, expand the recordset, and add the column binding to your page from the recordset

Sign in to reply to this post

Dennis

Thanks Jason, for clearing that up for me. I've wondered about that for some time.

The link you suggested above http://www.tizag.com/mysqlTutorial/mysqlsum.php (ps, they have very good tutorails) has the following example that I'm trying to understand how to use...

<?php
// Make a MySQL Connection

$query = "SELECT type, SUM(price) FROM products GROUP BY type";

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['type']. " = $". $row['SUM(price)'];
echo "<br />";
}
?>

Would I put the "SELECT type, SUM(price) FROM products GROUP BY type" in a recordset and then hand code everything under it?

Also, if COUNT & SUM are used in a recordset, how does that help my dilemma of having to many recordsets? Is it because they provide many more results (statistics) with a single recordset?

Because I want to pick & choose what statistics I display, I went searching for a way to stack COUNT statements, I came across this example of code on stackoverflow. but I was able to get it to work (in a recordset) and echo the aliases...

SELECT
SUM(CASE WHEN Mbr_Status = 'Regular' THEN 1 ELSE 0 END) StatusRegular,
SUM(CASE WHEN Mbr_Status = 'App' THEN 1 ELSE 0 END) StatusApplicant,
SUM(CASE WHEN Mbr_Status = 'None' THEN 1 ELSE 0 END) StatusNone,
SUM(CASE WHEN Mbr_Status = 'Club' THEN 1 ELSE 0 END) StatusClub
FROM member_accounts

Does this look like a good (reliable & efficient) statement? I'm asking because I don't have a clue as to what it is doing with the if statements.

Thanks

Sign in to reply to this post

Jason ByrnesWebAssist

create a recordset using the following SQL:

SELECT type, SUM(price) AS priceSum FROM products GROUP BY type


dont forget to add an alias SUM(price) AS priceSum

then use the bindings panel to echo the information to the page and a repeat region if the recordset returns more than one row.

  Also, if COUNT & SUM are used in a recordset, how does that help my dilemma of having to many recordsets?  



because you are creating one recordset to return the results that you had been trying to get using multiple recordsets.


this:
SELECT
SUM(CASE WHEN Mbr_Status = 'Regular' THEN 1 ELSE 0 END) StatusRegular,
SUM(CASE WHEN Mbr_Status = 'App' THEN 1 ELSE 0 END) StatusApplicant,
SUM(CASE WHEN Mbr_Status = 'None' THEN 1 ELSE 0 END) StatusNone,
SUM(CASE WHEN Mbr_Status = 'Club' THEN 1 ELSE 0 END) StatusClub
FROM member_accounts

looks OK, except for the fact that the alias's are missing the ass statement.

If you need more help in createing this, i would sugest signing up for a premiere support appointment:
http://www.webassist.com/premier_request.php

Sign in to reply to this post

Dennis

Thanks Jason, you have been very helpful and I appreciate it.

Dennis

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...