A
A
Alexander Tokmakov2014-09-10 06:22:40
PHP
Alexander Tokmakov, 2014-09-10 06:22:40

How to count the number of requests to the database?

Hello.
Interested in the optimal solution for counting the number of queries to the database.
it is possible on the example of mysql, mysqli, PDO.
What I want:
to display in the footer: "Number of requests: 5".
What I came up with:
create a $zapros variable
After each $zapros++ request
In the footer echo $zapros
Disadvantage:
1. one fine moment I forget to write a variable and the request will not be correct.
2. code duplication is bad!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FanatPHP, 2014-09-10
@FanatPHP

Considering that the figure is completely meaningless, it would be easiest to do
echo rand(10, 15);

D
Dmitry Entelis, 2014-09-10
@DmitriyEntelis

Instead of using mysqli in your code right away, make yourself some kind of wrapper library for these functions and call it.
As part of this library, already collect statistics, log slow queries, etc.

V
Vladimir Shishmintsev, 2014-09-10
@Pingvi

something like this

$sql_count = 0;
function mysql_myquery( $sql = "" )
{
    global $sql_count;
    
    ++$sql_count;
    return mysql_query($sql);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question