Page 1 of 1
Global Variable Question - probably simple
Posted: Wed Nov 28, 2007 8:10 pm
by kalvaris
I am trying to count how many queries I am doing each page. So, in a included file (that is included at the top of every page) has:
Code: Select all
global $totalqueries;
$totalqueries = '0';
Then under each mysql_query I have
However, when I try to increment $totalqueries from inside a function it does not incremement, thus when echoing $totalqueries at the bottom of the page it will only show the queries that were executed on the page (or included page) itself, and not the ones called from various functions that query the database.
Question: How can I increment a global variable from within a function, because:
Code: Select all
function someFunction()
{
$totalqueries++;
}
Isn't working.
Thanks!
Posted: Wed Nov 28, 2007 8:12 pm
by ianhull
Posted: Wed Nov 28, 2007 8:18 pm
by waradmin
(original post is mine, wrong name logged in for some reason).
When doing
Code: Select all
function checkMail()
{
...
...
global $totalqueries++;
...
}
I get:
Parse error: syntax error, unexpected T_INC, expecting ',' or ';' in /Users/steve/Sites/v2/functions.php on line 13
Posted: Wed Nov 28, 2007 8:26 pm
by phpBuddy
I dont think you can have INC++ in the global statement.
So use separate line, like this:
Code: Select all
function checkMail()
{
global $totalqueries, $username, $var2;
...
...
$totalqueries++;
...
$totalqueries++;
...
}
Usually we declare what variables will be 'global' in a first line of function.
Then those will stay global within the function.
Posted: Wed Nov 28, 2007 8:48 pm
by waradmin
Ok, I will try to rephrase:
Say I have a file index.php, which has 4 functions that each do database queries, I want to add up all queries executed across all 4 functions, then display that value on the index.php page. By doing global $totalqueries; in each function I am redeclaring it over and over, what I need is 1 global that I can increment from within various functions.
Posted: Wed Nov 28, 2007 8:50 pm
by ianhull
I would use a session to do this,
using a session would work virtually the same.
Code: Select all
function someFunction()
{
$_SESSION['totalqueries']++;
}
Posted: Wed Nov 28, 2007 8:56 pm
by waradmin
haha, thanks for the replies, believe it or not I just implemented that before I read this! What are the chances.
Posted: Wed Nov 28, 2007 8:59 pm
by ianhull
haha, great minds do think alike

Posted: Wed Nov 28, 2007 9:03 pm
by waradmin
ianhull wrote:haha, great minds do think alike

indeed they do.
Posted: Wed Nov 28, 2007 9:06 pm
by phpBuddy
ianhull wrote:I would use a session to do this,
using a session would work virtually the same.
Another way is to make one Class
with those 4 functions.
And so they would use the same class variable
Code: Select all
<?php
class QueryFunctions {
public $num_queries = 0;
public function1 ( $sql ) {
$this->num_queries++;
//.....
}
public function2 ( $sql ) {
$this->num_queries++;
//.....
}
public function3 ( $sql ) {
$this->num_queries++;
//.....
}
} // endclass
// START
$query = new QueryFunctions;
$sql = "SELECT * FROM table";
$result = $query->function1 ( $sql );
echo $query->num_queries;
?>
Posted: Wed Nov 28, 2007 11:01 pm
by Chris Corbyn
waradmin wrote:By doing global $totalqueries; in each function I am redeclaring it over and over, what I need is 1 global that I can increment from within various functions.
The "global" keyword will not recreate the global variable if it already exists. You can use global repeatedly.
Posted: Thu Nov 29, 2007 10:06 am
by feyd
"global" is bad, mmkay?
If you need globals inside a function, your design is likely flawed. Consider refactoring and refining further.