phpcms v9的debug函数

function usedtime(){
	$stime = explode(' ', SYS_START_TIME);
	$etime = explode(' ', microtime());
	return number_format(($etime[1] + $etime[0] - $stime[1] - $stime[0]), 6);
}
function debug(){
		if(!pc_base::load_config('system','debug') || defined('HTML')) return false;
	$myip = array('允许显示debug信息的ip地址');
	pc_base::load_sys_class('db_factory', '', 0);
	$sql_status = db_factory::get_instance();
	$mysqlserver = pc_base::load_config('database');//分布式sql
	$count = $querystr = array();
      foreach($mysqlserver AS $k=>$v){
	  $db_list = $sql_status->get_database($k);
	  $count[] = $k.':'. $db_list->querycount;
	  $querystr[] = $k.':'. implode(';<br />',$db_list->querystr);
	  }
	  $count = implode(';',$count);
      $querystr = implode(';',$querystr);
	  $output = '<table><tr><td>执行时间</td><td>'.usedtime().'</td></tr><tr><td>sql执行概况</td><td>'.$count.'</td></tr><tr><td>sql执行列表</td><td>'.$querystr.'</td></tr></table>';
	return in_array(ip(),$myip) ? $output : '';
}

 

 

如果采用了多数据库服务器,可以统计到所有服务器的查询次数.

 

另外如果想把执行的sql都打印出来,需要额外修改 mysql.class.php

增加变量定义 public $querystr = array();

并修改如下方法:

	private function execute($sql) {
		if(!is_resource($this->link)) {
			$this->connect();
		}
		$stime = explode(' ', microtime());
		$this->lastqueryid = mysql_query($sql, $this->link) or $this->halt(mysql_error(), $sql);
		$etime = explode(' ', microtime());
		$sec_time = number_format(($etime[1] + $etime[0] - $stime[1] - $stime[0]), 6);
        if(pc_base::load_config('system','debug')) $this->querystr[] = $sql.'('.$sec_time.')';
		$this->querycount++;
		return $this->lastqueryid;
	}

 

你可能感兴趣的:(phpcms)