<?
class T_DB
{
var $res, $num, $cnt, $row, $debug;
function T_DB()
{
$argc = func_num_args();
$argv = func_get_args();
$this->link_id= $GLOBALS['link_id'];
$this->debug = false;
}
function SQL($sql)
{
if (is_resource($this->link_id))
{
//$this->escape_string($sql);
$this->res = @mysql_query($sql,$this->link_id);
if($this->debug == true)
{
die(mysql_error());
}
if (is_resource($this->res))
{
$this->num = mysql_num_rows($this->res);
$this->cnt = $this->num;
$this->row = mysql_fetch_array($this->res);
return true;
}
$this->num = 0;
$this->cnt = $this->num;
$this->row = false;
return $this->res;
}else{
return false;
}
}
function SQL2($sql)
{
if (is_resource($this->link_id))
{
//$this->escape_string($sql);
$this->res2 = @mysql_query($sql,$this->link_id);
if (is_resource($this->res2)) {
$this->num2 = mysql_num_rows($this->res2);
$this->cnt2 = $this->num2;
$this->row2 = mysql_fetch_assoc($this->res2);
return true;
}
$this->num2 = 0;
$this->cnt2 = $this->num2;
$this->row2 = false;
return $this->res2;
}else
return false;
}
function ROW()
{
if (($argc = func_num_args()) == 0 || $this->row == false)
return;
$argv = func_get_args();
switch($argc) {
case 1 : print $this->row[$argv[0]];
break;
default: if ($argv[1] == -1)
$argv[1] = strlen($this->row[$argv[0]]);
if ($argc == 5)
ob_start();
for($i = 0; $i < $argv[1]; $i++) {
$ch = substr($this->row[$argv[0]], $i, 1);
print $ch;
if (ord($ch) >= 0x80) {
$i++;
$ch = substr($this->row[$argv[0]], $i, 1);
print $ch;
}
}
if ($argc == 5) {
$buffer = ob_get_contents();
ob_end_clean();
$buffer = preg_replace($argv[3], $argv[4], $buffer);
print $buffer;
}
if ($argc >= 3 && $i < strlen($this->row[$argv[0]]))
print $argv[2];
break;
}
}
function STEP()
{
if ($this->num == 0){
$this->row = false;
}else {
$this->num--;
$this->row = mysql_fetch_array($this->res);
}
}
function STEP2()
{
if ($this->num2 == 0)
$this->row2 = false;
else {
$this->num2--;
$this->row2 = mysql_fetch_assoc($this->res2);
}
}
function ID()
{
if (is_resource($this->link_id))
{
return mysql_insert_id();
}
return 0;
}
}
function BTAG()
{
$argv = func_get_args();
switch($argc)
{
case 1 : $GLOBALS['tt_db']= new T_DB($argv[0]);
break;
default: $GLOBALS['tt_db'] = new T_DB();
break;
}
}
function SQL($sql)
{
return $GLOBALS['tt_db']->SQL($sql);
}
function SQL2($sql)
{
return $GLOBALS['tt_db']->SQL2($sql);
}
function ROW($i)
{
return $GLOBALS['tt_db']->row == false ? false : $GLOBALS['tt_db']->row[$i];
}
function STEP()
{
$GLOBALS['tt_db']->STEP();
}
function STEP2()
{
$GLOBALS['tt_db']->STEP2();
}
function CNT()//取得结果集中行的数目
{
return $GLOBALS['tt_db']->cnt;
}
function CNT2()//取得结果集中行的数目
{
return $GLOBALS['tt_db']->cnt2;
}
function TOP()
{
return $GLOBALS['tt_db']->row;
}
function TOP2()
{
return $GLOBALS['tt_db']->row2;
}
function ID()//取得上一步 INSERT 操作产生的 ID
{
return $GLOBALS['tt_db']->ID();
}
function tt_enc($s) //加密
{
return str_replace("+","_",base64_encode(substr(bzcompress(rand(10, 99) . $s), 10)));
}
function tt_dec($s) //解密
{
return substr(bzdecompress('BZh41AY&SY' . base64_decode(str_replace("_","+",$s))), 2);
}
?>