● [Tutorial] How to see a list of all DB queries on a page

Sat Oct 08, 2022 9:35 am
Clan Leader
Top Dog
Nuke Dev / Coder
3043 Posts
coRpSE
In-Game
Once Human

Most Played:
This week: 161.4hrs.
Total Played: 196hrs.


  
Reputation: 7386.3
votes: 7
Well, a few weeks back, I was talking to Lonestar about trying to see all the queries that were being used on a particular module/page, and I was looking at coding that in for my personal use. He pointed out that it was already there and after some digging, he found it. I do find it useful to have for when developing, or trying to improve something, an easy way to see what a specific block/module is calling upon from the DB. Maybe it's something that I might be able to cache, or is already called upon, so there is no need for it to be called upon multiple times.

So for those of you like me that like to tinker and see this stuff, well, all you have to do is un comment something. Before I tell you what to uncomment, I will say this, I do not recommend just uncommenting it, and use the code I provide which will make it so only an admin can see it, and, if they call upon it from within the URL.

What to do.
Open public_html/mainfile.php

Find:

PHP:  [ Select all ]

//$db->debug = true; 


Replace with:

PHP:  [ Select all ]

if (is_admin() && isset($_GET['show-tables'])){
 
  Â $db->debug true;

 
Save & Close

Now, how to use it. It's quite simple actually, if you go onto your site and just say you go to your home page, now, if it has a /index.php or not, at the end, you would just put a ?show-tables and then hit enter, it will refresh to the page with the URL of YourSite.com?show-tables OR YourSite.com/index.php?show-tables. Once you put that in, at the bottom, in the footer, you should see all tables that are being used on that particular page. Just know that this only works if your page ends with a file or the domain.

Now, if you are looking at doing a module, for example, the Clan Manager, you would just go to the module, YourSite.com/modules.php?name=Clan_Manager, and at the end of that URL, add a &show-tables. By doing that, the URL would now look like: YourSite.com/modules.php?name=Clan_Manager&show-tables

An easy way to remember this is, if it ends with a .com, .net, .co.uk, ect..., or even as a PHP file, (.php), then use the question mark. Otherwise, use an & symbol. If you look at the URL right now of this page, it will look something like, (but not exact), MySite.com/modules.php?name=Forums&file=viewtopic&p=12 You will see that there is only one question mark, then multiple ampersands, (&). If the URL you want to see the tables on already has a question mark, then use the ampersand instead.

Now, the is_admin() in the code I provided is of course going to check to see you are logged in as admin first, and if you are not, you will not be able to see the tables. The next part of the code, the isset($_GET['show-tables']), check to see if you put the show-tables into the URL. If both of those conditions are met, then it will display the info, but only if those two conditions are met.

Now, who is this for. Well, mostly for those that are developing blocks/module, or for those that are tinkers or curious, or those that need the info to improve site speeds or help find hangups. Once done, you can by all means replace the // in front of the $db->debug, or, you can leave it as it can only be used by those that are admins. You could also make it specific only to your account, but if you want that information, just ask and I will post at a later time.

Thanks Lonestar for pointing this out. I personally do find it helpful.


Expand
Forums ©