PHP编程:某文本数据库blog作者写的文本数据库操...
咱们就开始学习动态语言的概念吧,刚一接触动态语言,可能很多人都会蒙了,怎么这乱七八糟的东西,在网页里显示的时候却是另外一码事?其实这并不算乱七八糟,你写的HTML代码不也一样是一堆堆的字符吗?毕竟,代码并不是作为直接输出的,而是经过处理的,说白了,HTML是经过HTML解析器,而PHP当然也就通过PHP解析器了,跟学习HTML一样的道理,想让任何的解析器完成操作,就必须使用它们专用的语法结构,所以PHP长相奇怪也就不足为奇了。数据|数据库 作者的1.0版:<?
/*
本代码开源,您可以对其停止修正.
上面文字请不要修正.
*********************************************
php文本数据库类1.0版
powerd by bpns
mysite:http://space.tju.cn/site/redboy/
2006-4-20
*********************************************
为了您的数据库平安,请在此法式中更改您的数据默许目次
将function TxtDB($name,$mod='w',$path='my_dbm')
中$path='my_dbm'修正成你本人设定的目次,如$path='abc123_dbm',
并将根目次下的my_dbm文件夹对行对应的改名.
*/
class TxtDB
{
var $name='';//文本数据库名
var $path='';
var $minLen=20;
var $isError;
var $dbh;
var $indxh;
var $lfth;
var $lckh;
var $rcdCnt=0;
var $maxID=0;
var $leftCnt=0;
var $DBend=0;
var $mod='w';
function TxtDB($name,$mod='w',$path='bpns_dbm')//修正数据库的默许文件夹在此修正
{
$this->name=$name;
$this->path=$path.'/'.$name;
$this->isError=0;
$this->mod=$mod;
$path=$this->path;
if ($name!='')
{
@mkdir($this->path,0777);
if (!file_exists($path.'/'.$name.'.tdb')) $this->dbh=fopen($this->path.'/'.$name.'.tdb','w+');
else $this->dbh=fopen($path.'/'.$name.'.tdb','r+');
if (!file_exists($path.'/'.$name.'.indx')) $this->indxh=fopen($this->path.'/'.$name.'.indx','w+');
else $this->indxh=fopen($path.'/'.$name.'.indx','r+');
if (!file_exists($path.'/'.$name.'.lft')) $this->lfth=fopen($this->path.'/'.$name.'.lft','w+');
else $this->lfth=fopen($this->path.'/'.$name.'.lft','r+');
if ($this->mod=='w')
{
$this->lckh=fopen($this->path.'/'.$name.'.lck','w');
flock($this->lckh,2);
fwrite($this->lckh,'lck');//lock the datebase
}
$rcd=$this->getRcd(0);
$this->rcdCnt=$rcd;
$this->maxID=$rcd;
$this->DBend=$rcd;
$rcd=$this->getLeft(0);
$this->leftCnt=$rcd;
}
else $this->isError=1;
}
function setRcd($rid,$id,$loc,$len)
{
fseek($this->indxh,$rid*12);
$str=pack('III',$id,$loc,$len);
fwrite($this->indxh,$str,12);
}
function getRcd($rid)
{
fseek($this->indxh,$rid*12);
$str=fread($this->indxh,12);
$rcd=array();
$rcd=str2int($str);
$rcd=str2int(substr($str,4,4));
$rcd=str2int(substr($str,8,4));
return $rcd;
}
function setLeft($lid,$loc,$len)
{
fseek($this->lfth,$lid*8);
$str=pack('II',$loc,$len);
fwrite($this->lfth,$str,8);
}
function getLeft($lid)
{
fseek($this->lfth,$lid*8);
$str=fread($this->lfth,8);
$rcd=str2int($str);
$rcd=str2int(substr($str,4,4));
return $rcd;
}
function clear()
{
$this->setRcd(0,0,0,0);
$this->setLeft(0,0,0);
}
function close()
{
@fclose($this->dbh);
@fclose($this->indxh);
@fclose($this->lfth);
@fclose($this->lckh);
}
function seekSpace($len)
{
$res=array('loc'=>0,'len'=>0);
if ($this->leftCnt<1) return $res;
$find=0;
$min=1000000;
for ($i=$this->leftCnt;$i>0;$i--)
{
$res=$this->getLeft($i);
if ($res==$len) {$find=$i;break;}
else if($res>$len)
{
if ($res-$len<$min)
{
$min=$res-$len;
$find=$i;
}
}
}
if ($find)
{
$res=$this->getLeft($find);
if ($res<2*$len)
{
fseek($this->lfth,($find+1)*8);
$str=fread($this->lfth,($this->leftCnt-$find)*8);
fseek($this->lfth,$find*8);
fwrite($this->lfth,$str);
$this->leftCnt--;
$this->setLeft(0,$this->leftCnt,0);
return $res;
}
else
{
$rs=array();
$rs=$res;
$rs=$len;
$res+=$len;
$this->setLeft($find,$res,$res-$len);
return $rs;
}
}
else//fail
{
$res=0;
return $res;
}
}
function insert($content,$len=0)//return with record id
{
$res=array('loc'=>0);
if ($this->mod!='w') return 0;
if (!$len) $len=strlen($content);
if ($len<$this->minLen) $len=$this->minLen;
if ($this->leftCnt) $res=$this->seekSpace($len);
if (!$res)
{
$res=$this->DBend;
$res=$len;
}
if ($res+$res>$this->DBend) $this->DBend=$res+$res;
//echo $this->DBend.'<br>';
$this->maxID++;
$this->rcdCnt++;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->setRcd($this->rcdCnt,$this->maxID,$res,$res);
fseek($this->dbh,$res);
fwrite($this->dbh,$content,$len);
return $this->maxID;
}
function findByID($id)
{
if ($id<1 or $id>$this->maxID or $this->rcdCnt<1) return 0;
$left=1;
$right=$this->rcdCnt;
while($left<$right)
{
$mid=(int)(($left+$right)/2);
if ($mid==$left or $mid==$right) break;
$rcd=$this->getRcd($mid);
if ($rcd==$id) return $mid;
else if($id<$rcd) $right=$mid;
else $left=$mid;
}
//$rcd=$this->getRcd($mid);
//if ($rcd==$id) return $mid;
$rcd=$this->getRcd($left);
if ($rcd==$id) return $left;
$rcd=$this->getRcd($right);
if ($rcd==$id) return $right;
return 0;
}
function delete($id)
{
if ($this->mod!='w') return 0;
$rid=$this->findByID($id);
if (!$rid) return;
$res=$this->getRcd($rid);
fseek($this->indxh,($rid+1)*12);
$str=fread($this->indxh,($this->rcdCnt-$i)*12);
fseek($this->indxh,$rid*12);
fwrite($this->indxh,$str);
$this->rcdCnt--;
if ($res+$res==$this->DBend)
{
$this->DBend=$res;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
}
else
{
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->leftCnt++;
$this->setLeft(0,$this->leftCnt,0);
$this->setLeft($this->leftCnt,$res,$res);
}
}
function update($id,$newcontent,$len=0)
{
if ($this->mod!='w') return;
$rid=$this->findByID($id);
if (!$rid) return;
if (!$len) $len=strlen($newcontent);
$rcd=$this->getRcd($rid);
if ($rcd<$len)
{
$this->leftCnt++;
$this->setLeft(0,$this->leftCnt,0);
$this->setLeft($this->leftCnt,$rcd,$rcd);
$rcd=$this->DBend;
$rcd=$len;
$this->DBend+=$len;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->setRcd($rid,$rcd,$rcd,$rcd);
}
fseek($this->dbh,$rcd);
fwrite($this->dbh,$newcontent,$len);
//echo $id.'<br>'.$content.'<br>'.$len;
}
function selectByRid($rid)
{
$res=array('id'=>0,'content'=>'');
if ($rid<1 or $rid>$this->rcdCnt) return $res;
else $rcd=$this->getRcd($rid);
$res=$rcd;
$res=$rcd;
fseek($this->dbh,$rcd);
$res=fread($this->dbh,$rcd);
return $res;
}
function select($id)
{
return $this->selectByRid($this->findByID($id));
}
function backup()
{
copy($this->path.'/'.$this->name.'.tdb',$this->path.'/'.$this->name.'.tdb.bck');
copy($this->path.'/'.$this->name.'.indx',$this->path.'/'.$this->name.'.indx.bck');
copy($this->path.'/'.$this->name.'.lft',$this->path.'/'.$this->name.'.lft.bck');
}
function recover()
{
copy($this->path.'/'.$this->name.'.tdb.bck',$this->path.'/'.$this->name.'.tdb');
copy($this->path.'/'.$this->name.'.indx.bck',$this->path.'/'.$this->name.'.indx');
copy($this->path.'/'.$this->name.'.lft.bck',$this->path.'/'.$this->name.'.lft');
}
}
?>
作者的2.0版,生成dbm:
<?
class TxtDB
{
var $name='';//文本数据库名
var $path='';
var $minLen=20;
var $isError;
var $dbh;
var $indxh;
var $lfth;
var $lckh;
var $rcdCnt=0;
var $maxID=0;
var $leftCnt=0;
var $DBend=0;
var $mod='w';
function TxtDB($name,$mod='w',$path='bpns_dbm')
{
$this->name=$name;
$this->path=$path.'/'.$name;
$this->isError=0;
$this->mod=$mod;
$path=$this->path;
if ($name!='')
{
@mkdir($this->path,0777);
if (!file_exists($path.'/'.$name.'.tdb')) $this->dbh=fopen($this->path.'/'.$name.'.tdb','w+');
else $this->dbh=fopen($path.'/'.$name.'.tdb','r+');
if (!file_exists($path.'/'.$name.'.indx')) $this->indxh=fopen($this->path.'/'.$name.'.indx','w+');
else $this->indxh=fopen($path.'/'.$name.'.indx','r+');
if (!file_exists($path.'/'.$name.'.lft')) $this->lfth=fopen($this->path.'/'.$name.'.lft','w+');
else $this->lfth=fopen($this->path.'/'.$name.'.lft','r+');
if ($this->mod=='w')
{
$this->lckh=fopen($this->path.'/'.$name.'.lck','w');
flock($this->lckh,2);
fwrite($this->lckh,'lck');//lock the datebase
}
$rcd=$this->getRcd(0);
$this->rcdCnt=$rcd;
$this->maxID=$rcd;
$this->DBend=$rcd;
$rcd=$this->getLeft(0);
$this->leftCnt=$rcd;
}
else $this->isError=1;
}
function setRcd($rid,$id,$loc,$len)
{
fseek($this->indxh,$rid*12);
$str=pack('III',$id,$loc,$len);
fwrite($this->indxh,$str,12);
}
function getRcd($rid)
{
fseek($this->indxh,$rid*12);
$str=fread($this->indxh,12);
$rcd=array();
$rcd=str2int($str);
$rcd=str2int(substr($str,4,4));
$rcd=str2int(substr($str,8,4));
return $rcd;
}
function setLeft($lid,$loc,$len)
{
fseek($this->lfth,$lid*8);
$str=pack('II',$loc,$len);
fwrite($this->lfth,$str,8);
}
function getLeft($lid)
{
fseek($this->lfth,$lid*8);
$str=fread($this->lfth,8);
$rcd=str2int($str);
$rcd=str2int(substr($str,4,4));
return $rcd;
}
function clear()
{
$this->setRcd(0,0,0,0);
$this->setLeft(0,0,0);
}
function close()
{
@fclose($this->dbh);
@fclose($this->indxh);
@fclose($this->lfth);
@fclose($this->lckh);
}
function seekSpace($len)
{
$res=array('loc'=>0,'len'=>0);
if ($this->leftCnt<1) return $res;
$find=0;
$min=1000000;
for ($i=$this->leftCnt;$i>0;$i--)
{
$res=$this->getLeft($i);
if ($res==$len) {$find=$i;break;}
else if($res>$len)
{
if ($res-$len<$min)
{
$min=$res-$len;
$find=$i;
}
}
}
if ($find)
{
$res=$this->getLeft($find);
if ($res<2*$len)
{
fseek($this->lfth,($find+1)*8);
$str=fread($this->lfth,($this->leftCnt-$find)*8);
fseek($this->lfth,$find*8);
fwrite($this->lfth,$str);
$this->leftCnt--;
$this->setLeft(0,$this->leftCnt,0);
return $res;
}
else
{
$rs=array();
$rs=$res;
$rs=$len;
$res+=$len;
$this->setLeft($find,$res,$res-$len);
return $rs;
}
}
else//fail
{
$res=0;
return $res;
}
}
function insert($content,$len=0)//return with record id
{
$res=array('loc'=>0);
if ($this->mod!='w') return 0;
if (!$len) $len=strlen($content);
if ($len<$this->minLen) $len=$this->minLen;
if ($this->leftCnt) $res=$this->seekSpace($len);
if (!$res)
{
$res=$this->DBend;
$res=$len;
}
if ($res+$res>$this->DBend) $this->DBend=$res+$res;
//echo $this->DBend.'<br>';
$this->maxID++;
$this->rcdCnt++;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->setRcd($this->rcdCnt,$this->maxID,$res,$res);
fseek($this->dbh,$res);
fwrite($this->dbh,$content,$len);
return $this->maxID;
}
function findByID($id)
{
if ($id<1 or $id>$this->maxID or $this->rcdCnt<1) return 0;
$left=1;
$right=$this->rcdCnt;
while($left<$right)
{
$mid=(int)(($left+$right)/2);
if ($mid==$left or $mid==$right) break;
$rcd=$this->getRcd($mid);
if ($rcd==$id) return $mid;
else if($id<$rcd) $right=$mid;
else $left=$mid;
}
//$rcd=$this->getRcd($mid);
//if ($rcd==$id) return $mid;
$rcd=$this->getRcd($left);
if ($rcd==$id) return $left;
$rcd=$this->getRcd($right);
if ($rcd==$id) return $right;
return 0;
}
function delete($id)
{
if ($this->mod!='w') return 0;
$rid=$this->findByID($id);
if (!$rid) return;
$res=$this->getRcd($rid);
fseek($this->indxh,($rid+1)*12);
$str=fread($this->indxh,($this->rcdCnt-$i)*12);
fseek($this->indxh,$rid*12);
fwrite($this->indxh,$str);
$this->rcdCnt--;
if ($res+$res==$this->DBend)
{
$this->DBend=$res;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
}
else
{
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->leftCnt++;
$this->setLeft(0,$this->leftCnt,0);
$this->setLeft($this->leftCnt,$res,$res);
}
}
function update($id,$newcontent,$len=0)
{
if ($this->mod!='w') return;
$rid=$this->findByID($id);
if (!$rid) return;
if (!$len) $len=strlen($newcontent);
$rcd=$this->getRcd($rid);
if ($rcd<$len)
{
$this->leftCnt++;
$this->setLeft(0,$this->leftCnt,0);
$this->setLeft($this->leftCnt,$rcd,$rcd);
$rcd=$this->DBend;
$rcd=$len;
$this->DBend+=$len;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->setRcd($rid,$rcd,$rcd,$rcd);
}
fseek($this->dbh,$rcd);
fwrite($this->dbh,$newcontent,$len);
//echo $id.'<br>'.$content.'<br>'.$len;
}
function selectByRid($rid)
{
$res=array('id'=>0,'content'=>'');
if ($rid<1 or $rid>$this->rcdCnt) return $res;
else $rcd=$this->getRcd($rid);
$res=$rcd;
$res=$rcd;
fseek($this->dbh,$rcd);
$res=fread($this->dbh,$rcd);
//$res=$rid;
return $res;
}
function select($id)
{
return $this->selectByRid($this->findByID($id));
}
function backup()
{
copy($this->path.'/'.$this->name.'.tdb',$this->path.'/'.$this->name.'.tdb.bck');
copy($this->path.'/'.$this->name.'.indx',$this->path.'/'.$this->name.'.indx.bck');
copy($this->path.'/'.$this->name.'.lft',$this->path.'/'.$this->name.'.lft.bck');
}
function recover()
{
copy($this->path.'/'.$this->name.'.tdb.bck',$this->path.'/'.$this->name.'.tdb');
copy($this->path.'/'.$this->name.'.indx.bck',$this->path.'/'.$this->name.'.indx');
copy($this->path.'/'.$this->name.'.lft.bck',$this->path.'/'.$this->name.'.lft');
}
}
?>
当然你可以把你最基本的功能放出来的时候就放出来,比如放到论坛上,让大家都参与, 本文当是我的笔记啦,遇到的问题随时填充 有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。 使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的 兴趣是最好的老师,百度是最好的词典。 使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的 实践是检验自己会不会的真理。 有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。 曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线\\\\\\\'_\\\\\\\' ; 最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。 我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能: 使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的 建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。 首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。 装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。 在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、 做为1门年轻的语言,php一直很努力。 Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81 当然这种网站的会员费就几十块钱。
页:
[1]