views:

179

answers:

3

What is the code to show how long it took for PHP to process a page?

Also, how many sql queries were performed to generate that page?

+4  A: 

See Expample #2 on http://cz.php.net/microtime You basically read the current time at the top and bottom of the page, and display the difference.

nos
+4  A: 

For the first question.

$start = microtime(true);
$end = microtime(true);

printf("Page was generated in %f seconds", $end - $start);

The second one is a little more complicated. You need an shared gateways that keeps tracks of all your queries and stores an execution counter. The most simple example can be a wrapper method around the mysql_query that increments a static variable and then passes the query to mysql_query.

The most part of modern ORM implements that feature, if you already use one of them.

Simone Carletti
A: 

For the query count, in this mysql class i have made, you would echo $m->qcount; to display how many querys have been run.

<?php

class mysql
{
      var $db_connection = 0;
      var $qcount = 0;

      function connect( $host='', $user='', $password='', $database='' )
      {
            $this->db_connection = @mysql_connect( $host, $user, $password );

            if(!$this->db_connection)
            {
                  die("Cannot connect to mySQL Host.");
            }
            else
            {
                  $select = mysql_select_db($database, $this->db_connection);

                  if(!$select)
                  {
                        die("Cannot connect to DB.");
                  }
                  else
                  {
                        return $select;
                  }
            }
      }

      function query($info)
      {
            $this->qcount++;

            return mysql_query($info, $this->db_connection);
      }

      function fetch($info)
      {
            return mysql_fetch_array($info);
      }
}

$m = new mysql;

$m->connect("hostname","username","password","database");

$m->query("blah blah blah");
$m->query("blah blah blah");

echo $m->qcount; // returns a count of 2

?>
Paul Janaway