Karim Ali
Karim Ali

Reputation: 61

How to count how many queries on page load?

I used this simple following function for my SQL select query:-

I want to be able to count how many queries is being executed when running this function? If this function has been called 10 times then it will be calling SQL query 10 times. How can I output this as string? I tried using

count($sql); 

but this produces..

1111111

Which means 7 times, when I try use

array_sum()

it doesn't add all the ones.. any help with this please?

many thanks

public function select($rows = '*', $table, $where = null, $order = null, $limit = null, $echo = null) {
    $sql = "SELECT ".$rows." FROM {".$table."}";
    if($where != null) {
        $sql .= " WHERE ".$where;
    }
    if($order != null) {
        $sql .= " ORDER BY ".$order;
    }
    if($limit != null) {
        $sql .= " LIMIT ".$limit;
    }
    if($echo != null) {
        echo $sql . '<br />';
    }
    //echo $sql . '<br />';
    echo count($sql);           
    return $sql;
}

Upvotes: 0

Views: 814

Answers (3)

Pieter
Pieter

Reputation: 1833

You could work with a static variable that can be accessed within and outside the function.

public function select (...) {

    static $count_sql;

    [your function code]

    $count_sql++;

}

Upvotes: 1

Kibo
Kibo

Reputation: 888

you can use static variable inside your function and increase it each time.

Upvotes: 2

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

The simplest approach would be to wrap your SQL queries into class/function and plant accounting there. Then you just need to init your counter as very first thing in your script. Increase counter on each query. Display counter at the end of your scripts.

But in your case, if your return string "1111" means 4 queries (like each character means single query), then just do ordinary strlen() on that string and you are done.

Upvotes: 2

Related Questions