


PHP gets the number of database queries to generate a page_PHP tutorial
很多博客软件都有这么一个功能,比如“生成本次页面一共花费了xx毫秒,进行了xx次数据库查询”等等。那么这个功能是如何实现的呢,下面我大概说下思路。
1. 在类的构造函数中声明全局变量
定义一个全局变量 $queries 用来统计页面生成经过的数据库查询次数。
function __construct() { parent::__construct(); global $queries; }
2. 修改数据库类中封装好的的 query()
你应该有用到数据库类吧,找到它封装 query() 的方法,比如下面的:
// 执行SQL语句 public function query($query) { //echo $query.'<br />'; ++$GLOBALS['queries']; return $this->result = mysql_query($query, $this->link); }
那么每执行一次 Query,全局变量 queries 就会自增1。
3. 在方法体中这样写:
public function content($id = 0) { $GLOBALS['queries'] = 0; // something to do echo $GLOBALS['queries']; }
就这么简单就能实现那个功能了。
4. 附带计算PHP脚本执行的函数
之前写的博文介绍了下计算PHP脚本执行时间的函数,这里再贴一下吧。
// 计时函数 public function runtime($mode = 0) { static $t; if(!$mode) { $t = microtime(); return; } $t1 = microtime(); //list($m0,$s0) = split(" ",$t); list($m0,$s0) = explode(" ",$t); //list($m1,$s1) = split(" ",$t1); list($m1,$s1) = explode(" ",$t1); return sprintf("%.3f ms",($s1+$m1-$s0-$m0)*1000); }
使用如下:
public function content($id = 0) { $this -> runtime(); $GLOBALS['queries'] = 0; // something to do echo $GLOBALS['queries']; echo $this -> runtime(1); }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:
