简体   繁体   中英

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

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.

您可以在函数内部使用静态变量,并每次增加它。

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM