MYSQL在PHP中的运用(操作类文件)

<?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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值