php 如何输出DB类上所有SQL语句,页面上的所有 有关mysql 连接,语句 等信息
发布网友
发布时间:2022-04-06 01:55
我来回答
共1个回答
热心网友
时间:2022-04-06 03:25
class DB_MYSQL
{
/*
* @_conf Array: [hostname],[username],[password],[dataname],[dbtype],[charset],[debug]
*/
var $_conf;
var $_dblink; //mysql connect
var $_dberr =0;
var $_sqlnum=0; //SQL query times
var $_sql='';
var $pageinfo =array();
var $pageSize =20; //=0: all; >0 page size
var $pageOrder=0;
var $stripSet =0;
function DB_MYSQL($db_config=''){
if(empty($db_config)){
global $db_conf;
$db_config=$db_conf[0];
}
$this->_conf=$db_config;
}
function conn()
{
$this->_dblink=@mysql_connect($this->_conf['hostname'],$this->_conf['username'], $this->_conf['password'])
OR $this->debug("mysql_connect error:".str_replace($this->_conf['username'].'@','',mysql_error($this->_dblink)));
if($this->_dblink)
{
if (isset($this->_conf['charset']) and strlen($this->_conf['charset'])>0)
{
@mysql_query("SET character_set_connection=".$this->_conf['charset'].", character_set_results=".$this->_conf['charset'].", character_set_client=binary", $this->_dblink);
}
$res=@mysql_select_db($this->_conf['dataname'],$this->_dblink)
OR $this->debug("mysql_select_db error:".mysql_error($this->_dblink));
if ($res) return true;
}
$this->_dberr=1;
return false;
}
function select_db($NewDB){
$this->_conf['dataname']=$NewDB;
if(empty($this->_dblink)){
return $this->conn();
}
$res=@mysql_select_db($this->_conf['dataname'])
OR $this->debug("mysql_select_db error:".mysql_error($this->_dblink));
if ($res) return true;
$this->_dberr=1;
return false;
}
/*
* @array query ($fetchType>0)
* @result query ($fetchType=0)
*/
function query($str,$fetchType=0,$file_rows='')
{
if (strlen($str)<1){
return false;
}
if(!$this->_dblink){
$this->conn();
}
else if (!mysql_ping($this->_dblink))
{
$this->close();
$this->conn();
}
$SQL=substr($str,0,18).'..';
if ($this->_conf['debug'] > 2)
{
$SQL=$str;
}
if(!empty($file_rows))
{
$SQL=$SQL.'<BR>'.$file_rows;
}
$this->_sql=$str;
$result=@mysql_query($str,$this->_dblink) OR $this->debug("mysql_query error:".mysql_error(),$SQL);
$this->_sqlnum=$this->_sqlnum+1;
if ($result)
{
if ($fetchType==1)
{
$arr=@mysql_fetch_array($result,1);
$this->check_query_quote($arr);
$this->free($result);
return $arr;
}
else if ($fetchType==2)
{
$arr = array();
while ($row = mysql_fetch_array($result,1))
{
$this->check_query_quote($row);
$arr[] = $row;
}
$this->free($result);
return $arr;
}
return $result;
}
else if ($fetchType>1) # NEW 2010-7-28
{
return array();
}
return false;
}//END sql
/*
* @array query_page
*/
function query_page(&$page,$Field,$Table,$Where='',$Order='',$Group='')
{
$page=intval($page);
$psize=abs($this->pageSize);
$this->_table=$Table;
if($psize<0 or $psize>300) $psize=20;
$sql="SELECT count(*) as allnum FROM $Table $Where";
if (strlen($Group)>0) {
$this->_sql="$sql $Group";
$result=$this->query("$sql $Group");
$rowsnum=$this->num_rows($result);
}
else{
$this->_sql=$sql;
$row=$this->query($sql,1);
$rowsnum=$row['allnum']+0;
}
if($rowsnum==0){
$page=0;
$this->pageinfo=Array('page'=>0,'pagenum'=>0,'rowsnum'=>0);
return array();
}
$pagenum=$psize<1 ? 1 : ceil($rowsnum/$psize);
$limit='';
if($psize==0){
$page=1;
$pagenum=1;
}
else
{
if($page > $pagenum or $page < 1) $page=1;
if($pagenum > 1){
$limit='limit '.(($page-1)*$psize).','.$psize;
}
}
$this->pageinfo['page'] =$page;
$this->pageinfo['pagenum'] =$pagenum;
$this->pageinfo['rowsnum'] =$rowsnum;
$sql ="SELECT $Field FROM $Table $Where $Group $Order $limit";
$this->_sql=$sql;
return $this->query($sql,2);
}
/*
* @array fetch_array
* @type: 1 MYSQL_ASSOC 2 MYSQL_NUM #3 MYSQL_BOTH
*/
function fetch_array(&$result,$type=1)
{
$arr=Array();
if (!$result) return $arr;
while ($row = @mysql_fetch_array($result,$type))
{
$this->check_query_quote($row);
return $row;
}
$this->free($result);
return false;
}
function fetch_subarr(&$result,$keyset='',$valset='')
{
$arr=Array();
if (!$result) return $arr;
if (empty($keyset))
{
return $arr;
}
while ($row = @mysql_fetch_array($result,1))
{
$this->check_query_quote($row);
if (!empty($keyset) and isset($row[$keyset]))
{
$tag=$row[$keyset];
$arr[$tag]=empty($valset) ? $row : $row[$valset];
}
}
$this->free($result);
return $arr;
}
function check_query_quote(&$val)
{
if($this->stripSet) $this->stripVar($val);
}
function stripVar(&$val)
{
if(is_array($val)){
foreach($val as $key => $v)
{
$this->stripVar($val[$key]);
}
}
else if(is_string($val)){
$val=StripSlashes($val);
}
}
function num_rows($result){
return @mysql_num_rows($result);
}
function affected_rows()
{
return @mysql_affected_rows($this->_dblink);
}
function data_seek(&$result,$row_num=0){
return @mysql_data_seek($result,$row_num);
}
function free(&$result){
return @mysql_free_result($result);
}
function last_id(){
$id=@mysql_insert_id($this->_dblink);
return intval($id);
}
function err_code()
{
return @mysql_errno($this->_dblink);
}
function err_msg()
{
return mysql_error($this->_dblink);
}
function close(){
$result = @mysql_close($this->_dblink);
$this->_dblink=0;
}
/* NEW 2010-8-15 */
function getsql($val,$type=0)
{
if($type==1) {
$g='';
if(substr($val,0,1)=='-') $g='-';
$val=preg_replace('/\D/','',$val);
if(strlen($val)==0) $val=0;
return $g.$val;
}
else if($type==2) return doubleval($val);
else if($type==3) return $val;
else{
if ($this->_dblink) return "'" . mysql_real_escape_string($val) . "'";
else return "'" . mysql_escape_string($val) . "'";
}
}
function getstr($val,$type=0) {
return $this->getsql($val,$type);
}
function debug($string,$SQL='')
{
$debug=$this->_conf['debug'];
if ($debug==4) {
die($string.'<HR>'.$SQL);
}
else if ($debug==3) {
print($string.'<HR>'.$SQL);
}
else if ($debug==2) {
print('<!--'.$string.'<HR>'.$SQL.'-->');
}
else if ($debug==1) {
//$code=intval($this->err_code());
return 0;
}
return 0;
}
}