网站访问数据库 每个页面统计执行的查询语句 详细信息 便于页面数据访问优化 -----自制脚本

一 : 代码效果分析与说明:

   1:写在访问文件对应的名称脚本中

   

 

    2:访问php文件获取文件名

      

       

 

    3:执行echo 出现的效果

    

 

 

 

 

 

 

 

 

            4:核心代码说明

 

            

 

    二:代码核心分析

      1: 要写在查询的方法里,这样这个方法在其他界面有引进,就执行这个写入txt文件的这个功能了 

          

 function query($sql, $type = '')
    {
        if ($this->link_id === NULL)
        {
            $this->connect($this->settings['dbhost'], $this->settings['dbuser'], $this->settings['dbpw'], $this->settings['dbname'], $this->settings['charset'], $this->settings['pconnect']);
            $this->settings = array();
        }

        if ($this->queryCount++ <= 99)
        {
            $this->queryLog[] = $sql;
        }
        if ($this->queryTime == '')
        {
            if (PHP_VERSION >= '5.0.0')
            {
                $this->queryTime = microtime(true);
            }
            else
            {
                $this->queryTime = microtime();
            }
        }

        /* 当当前的时间大于类初始化时间的时候,自动执行 ping 这个自动重新连接操作 */
        if (PHP_VERSION >= '4.3' && time() > $this->starttime + 1)
        {
            mysql_ping($this->link_id);
        }
        $this->pingping();
        //strtoupper(substr($sql, 0 , 6)) == 'SELECT'
        //if(REAL_HTML
        echo $sql."<br/>";

        /*
         * PHP_EOL 就相当于html中的<br/>
         * FILE_APPEND|LOCK_EX 表示写入方式 FILE_APPEND:避免删除已有的文件内容 有安全的写入二进制数据
         *
         * $sql 写在什么query insert delete update 类中的方法中很重要 :file_put_contents 写在那个方法中,就会全站程序执行此方法时候 来写入txt文件中
         * */
        file_put_contents(ROOT_PATH.'temp/'.PHP_SELF.'.txt', '时间:'.date('Y-m-d h:i:s',time()).' || '.$sql.PHP_EOL, FILE_APPEND|LOCK_EX);
        
    
        if (!($query = mysql_query($sql, $this->link_id)) && $type != 'SILENT')
        {
            $this->error_message[]['message'] = 'MySQL Query Error';
            $this->error_message[]['sql'] = $sql;
            $this->error_message[]['error'] = mysql_error($this->link_id);
            $this->error_message[]['errno'] = mysql_errno($this->link_id);

            $this->ErrorMsg();
            return false;
        }

        if (defined('DEBUG_MODE') && (DEBUG_MODE & 8) == 8)
        {
            $logfilename = $this->root_path . DATA_DIR . '/mysql_query_' . $this->dbhash . '_' . date('Y_m_d') . '.log';
            $str = $sql . "\n\n";

            if (PHP_VERSION >= '5.0')
            {
                file_put_contents($logfilename, $str, FILE_APPEND);
            }
            else
            {
                $fp = @fopen($logfilename, 'ab+');
                if ($fp)
                {
                    fwrite($fp, $str);
                    fclose($fp);
                }
            }
        }

        return $query;
    }


     核心操作文件类文件:http://pan.baidu.com/s/1jHRfJPc

    

 

 

 

  

posted @ 2017-03-08 16:37  王传明  阅读(184)  评论(0)    收藏  举报