<?php
//一般用法:
$gDB = new MyDB_Sql($gMysql_host_name, $gMysql_db_name, $gMysql_user_name, $gMysql_user_password); #数据库连接
$sql="select * from tablename";
$gDb->query($sql);
$num=$gDb->num_rows(); //有效数据数
$gDb->next_record();
$fields=$gDb->Record["FIELD"]; //取字段内容
?>
[@more@]if(!isset($dbclass__))
{
$dbclass__ = true; // escape redefine
// require_once("../include/WriteLog.php"); //写日志函数
class MyDB_Sql
{
var $Host = "";
var $lDatabase = "";
var $lUser = "";
var $lPassword = "";
var $lPort="3306";
var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row;
var $Errno = 0;
var $Error = "";
var $Auto_free = 0; ## Set this to 1 for automatic mysql_free_result()
var $Auto_commit = 0; ## set this to 1 to automatically commit results
var $debugmode = 0;
function MyDB_Sql($lHostName,$lDatabase,$lUser,$lPassword,$lPort="3306")
//constructor funtcion
{
$this->Host=$lHostName;
$this->Database=$lDatabase;
$this->User=$lUser;
$this->Password=$lPassword;
$this->Port=$lPort;
}
function connect()
{
if($this->Link_ID == 0)
{
$this->Link_ID = mysql_connect($this->Host.":".$this->Port, $this->User, $this->Password);
if (!$this->Link_ID)
{
$this->halt("Link_ID == false, 连接失败!");
}
$SelectResult = mysql_select_db($this->Database, $this->Link_ID);
if(!$SelectResult)
{
$this->Errno = mysql_errno($this->Link_ID);
$this->Error = mysql_error($this->Link_ID);
$this->halt("不能选定数据库".$this->Database."");
}
}
}
function query($lSql)
{
global $gProgCode;//M05 hxr 2003-11-5
#M04 hxr 2003-10-28 begin
if (eregi("^.+[Ff][Rr][Oo][Mm][a-z, fnrtv]{0,}ccg[, fnrtv]+.+$",$lSql) &&
eregi("^[ fnrtv]{0,}[Ss][Ee][Ll][Ee][Cc][Tt][ fnrtv]+.+$",$lSql) && $gProgCode)//M05 hxr 2003-11-5
{
global $sTradeId,$sTradeSet,$sAreaId,$sAreaSet;
$lCondition = " (ccg42 in $sTradeSet or ccg42 is null) and (ccg41 in ".$sAreaSet." or ccg41 is null) and (ccg50 not in ('3') or ccg50 is null) and ";
if ($sTradeId!=-1 && $sTradeId)
$lCondition .= " (ccg42 = '$sTradeId' or ccg42 is null) and ";
if ($sAreaId!=-1 && $sAreaId)
$lCondition .= " (ccg41 = '$sAreaId' or ccg41 is null) and ";
$lSql=preg_replace("/([s]+where[s]+)/i", " 1 ".$lCondition." ", $lSql);
}
#M04 hxr 2003-10-28 end
$this->connect();
WriteLog("[SQL]: ", $lSql,">>","../temp/log.txt");
if ($this->debugmode)
printf("Debug: query = %s
n", $lSql);
$this->Query_ID = mysql_query($lSql,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->Query_ID)
{
$this->halt("Invalid SQL: ".$lSql);
}
return $this->Query_ID;
}
function next_record()
{
$this->Record = @mysql_fetch_array($this->Query_ID);
//for ($lI=0;$lIRecord);$lI++)
// $this->Record[$lI]=htmlspecialchars($this->Record[$lI]);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$lStat = is_array($this->Record);
if (!$lStat && $this->Auto_free)
{
mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $lStat;
}
function seek($lPos)
{
$lStatus = mysql_data_seek($this->Query_ID, $lPos);
if ($lStatus)
$this->Row = $lPos;
return $lStatus;
}
function affected_rows()
{
return mysql_affected_rows($this->Link_ID);
}
function num_rows()
{
return @mysql_num_rows($this->Query_ID);
}
function p($lName)
{
print $this->Record[$lName];
}
function insert_id()
{
return mysql_insert_id($this->Link_ID);
}
function halt($lMsg)
{
$lErrText1="数据库错误:".$lMsg.";";
$lErrText2="MySQl错误:".$this->Errno.":".$this->Error.".";
//echo "";
}
}
}
?>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9812031/viewspace-915117/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9812031/viewspace-915117/