/    Sign up×
Community /Pin to ProfileBookmark

Number of queries executed?…

How would I be able to find out the number of queries executed in a script? I saw that many pages (especially forums) have in the footer “This page took xx.xx seconds to generate with xx queries”. Is there a php function or mysql function that tells me this or do I have to manually count each query?…

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 09.2007 — As far as I know, you'd have to count it yourself. I suppose you could make your own wrapper function that would both execute the query and increment a counter variable, which might save you a little typing in the long run.
Copy linkTweet thisAlerts:
@pcthugApr 10.2007 — I'd suggest either;

Using a class and increment a private property:[code=php]
<?php

class Query
{
/**
* Number of queries counted
* thus far
*/
private $count = 0;

/**
* Constructor
*
* @return void
*/
public function __construct() {}

/**
* Execute Db query and
* increment counter
*
* @param string $query
* @param resource $link_identifier
* @return resource
*/
public function query($query, $link_identifer = NULL)
{
$this->incrementCounter();

return is_null($link_identifier) ? mysql_query($query) : mysql_query($query, $link_identifier);
}

/**
* Increment Query Counter
*
* @return void
*/
private function incrementCounter()
{
$this->count++;
}

/**
* Get Query Count
*
* @return int
*/
public function getCount()
{
return $this->count;
}
}


/**
* BRIEF EXPLANATION OF USE
*/
$query = new Query;

$result = $query->query('CALL ALL OF YOUR QUERIES LIKE THIS');

echo 'Num of queries: ' . $query->getCount();[/code]


Or using and incrementing a static variable within the function scope:
[code=php]
function query($query, $link_identifer = NULL, $return_count = false)
{
static $count;

if ($return_count)
{
return $count;
}

$count++;

return is_null($link_identifier) ? mysql_query($query) : mysql_query($query, $link_identifier);
}

/**
* BRIEF EXPLANATION OF USE
*/
$result = query('CALL ALL OF YOUR QUERIES LIKE THIS');

echo 'Num of queries: ' . query(0, 0, true);[/code]
×

Success!

Help @Znupi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...