Global Variable Question - probably simple

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kalvaris
Forum Newbie
Posts: 5
Joined: Fri Nov 12, 2004 1:03 pm

Global Variable Question - probably simple

Post 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

Code: Select all

$totalqueries++;
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!
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all

//function
global $totalqueries++
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post 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.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post 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']++; 
}
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

haha, thanks for the replies, believe it or not I just implemented that before I read this! What are the chances.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

haha, great minds do think alike :D
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

ianhull wrote:haha, great minds do think alike :D
indeed they do.
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post 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;

?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"global" is bad, mmkay?

If you need globals inside a function, your design is likely flawed. Consider refactoring and refining further.
Post Reply