仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1039|回复: 18
打印 上一主题 下一主题

[学习教程] PHP编程:某文本数据库blog作者写的文本数据库操...

[复制链接]
若相依 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:09:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
咱们就开始学习动态语言的概念吧,刚一接触动态语言,可能很多人都会蒙了,怎么这乱七八糟的东西,在网页里显示的时候却是另外一码事?其实这并不算乱七八糟,你写的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[id];
    $this->maxID=$rcd[loc];
    $this->DBend=$rcd[len];
    $rcd=$this->getLeft(0);
    $this->leftCnt=$rcd[loc];
    }
    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[id]=str2int($str);
    $rcd[loc]=str2int(substr($str,4,4));
    $rcd[len]=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[loc]=str2int($str);
    $rcd[len]=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]==$len) {$find=$i;break;}
      else if($res[len]>$len)
      {
         if ($res[len]-$len<$min)
         {
           $min=$res[len]-$len;
           $find=$i;
         }
      }
    }
    if ($find)
    {
      $res=$this->getLeft($find);
      if ($res[len]<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[loc]=$res[loc];
        $rs[len]=$len;
        $res[loc]+=$len;
        $this->setLeft($find,$res[loc],$res[len]-$len);
        return $rs;
      }
    }
    else//fail
    {
      $res[len]=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[len])
    {
      $res[loc]=$this->DBend;
      $res[len]=$len;
    }
    if ($res[loc]+$res[len]>$this->DBend) $this->DBend=$res[loc]+$res[len];
    //echo $this->DBend.'<br>';
    $this->maxID++;
    $this->rcdCnt++;
    $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    $this->setRcd($this->rcdCnt,$this->maxID,$res[loc],$res[len]);
    fseek($this->dbh,$res[loc]);
    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]==$id) return $mid;
      else if($id<$rcd[id]) $right=$mid;
      else $left=$mid;
    }
    //$rcd=$this->getRcd($mid);
    //if ($rcd[id]==$id) return $mid;
    $rcd=$this->getRcd($left);
    if ($rcd[id]==$id) return $left;
    $rcd=$this->getRcd($right);
    if ($rcd[id]==$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[loc]+$res[len]==$this->DBend)
    {
      $this->DBend=$res[loc];
      $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[loc],$res[len]);
    }
  }
  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]<$len)
    {
      $this->leftCnt++;
      $this->setLeft(0,$this->leftCnt,0);
      $this->setLeft($this->leftCnt,$rcd[loc],$rcd[len]);
      $rcd[loc]=$this->DBend;
      $rcd[len]=$len;
      $this->DBend+=$len;
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
      $this->setRcd($rid,$rcd[id],$rcd[loc],$rcd[len]);
    }
    fseek($this->dbh,$rcd[loc]);
    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[id]=$rcd[id];
    $res[len]=$rcd[len];
    fseek($this->dbh,$rcd[loc]);
    $res[content]=fread($this->dbh,$rcd[len]);
    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[id];
    $this->maxID=$rcd[loc];
    $this->DBend=$rcd[len];
    $rcd=$this->getLeft(0);
    $this->leftCnt=$rcd[loc];
    }
    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[id]=str2int($str);
    $rcd[loc]=str2int(substr($str,4,4));
    $rcd[len]=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[loc]=str2int($str);
    $rcd[len]=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]==$len) {$find=$i;break;}
      else if($res[len]>$len)
      {
         if ($res[len]-$len<$min)
         {
           $min=$res[len]-$len;
           $find=$i;
         }
      }
    }
    if ($find)
    {
      $res=$this->getLeft($find);
      if ($res[len]<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[loc]=$res[loc];
        $rs[len]=$len;
        $res[loc]+=$len;
        $this->setLeft($find,$res[loc],$res[len]-$len);
        return $rs;
      }
    }
    else//fail
    {
      $res[len]=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[len])
    {
      $res[loc]=$this->DBend;
      $res[len]=$len;
    }
    if ($res[loc]+$res[len]>$this->DBend) $this->DBend=$res[loc]+$res[len];
    //echo $this->DBend.'<br>';
    $this->maxID++;
    $this->rcdCnt++;
    $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    $this->setRcd($this->rcdCnt,$this->maxID,$res[loc],$res[len]);
    fseek($this->dbh,$res[loc]);
    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]==$id) return $mid;
      else if($id<$rcd[id]) $right=$mid;
      else $left=$mid;
    }
    //$rcd=$this->getRcd($mid);
    //if ($rcd[id]==$id) return $mid;
    $rcd=$this->getRcd($left);
    if ($rcd[id]==$id) return $left;
    $rcd=$this->getRcd($right);
    if ($rcd[id]==$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[loc]+$res[len]==$this->DBend)
    {
      $this->DBend=$res[loc];
      $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[loc],$res[len]);
    }
  }
  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]<$len)
    {
      $this->leftCnt++;
      $this->setLeft(0,$this->leftCnt,0);
      $this->setLeft($this->leftCnt,$rcd[loc],$rcd[len]);
      $rcd[loc]=$this->DBend;
      $rcd[len]=$len;
      $this->DBend+=$len;
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
      $this->setRcd($rid,$rcd[id],$rcd[loc],$rcd[len]);
    }
    fseek($this->dbh,$rcd[loc]);
    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[id]=$rcd[id];
    $res[len]=$rcd[len];
    fseek($this->dbh,$rcd[loc]);
    $res[content]=fread($this->dbh,$rcd[len]);
    //$res[rid]=$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');
  }
}

?>


当然你可以把你最基本的功能放出来的时候就放出来,比如放到论坛上,让大家都参与,
柔情似水 该用户已被删除
沙发
发表于 2015-2-4 09:39:41 | 只看该作者
本文当是我的笔记啦,遇到的问题随时填充
admin 该用户已被删除
板凳
发表于 2015-2-9 02:09:28 | 只看该作者
有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。
谁可相欹 该用户已被删除
地板
发表于 2015-2-11 08:26:57 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
莫相离 该用户已被删除
5#
发表于 2015-2-12 08:49:15 | 只看该作者
兴趣是最好的老师,百度是最好的词典。
只想知道 该用户已被删除
6#
发表于 2015-2-24 05:29:50 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
再见西城 该用户已被删除
7#
发表于 2015-3-7 11:02:56 | 只看该作者
实践是检验自己会不会的真理。
透明 该用户已被删除
8#
发表于 2015-3-8 00:17:25 | 只看该作者
有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。
再现理想 该用户已被删除
9#
发表于 2015-3-16 13:49:57 | 只看该作者
曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线  \\\\\\\'_\\\\\\\' ;
山那边是海 该用户已被删除
10#
发表于 2015-3-18 17:18:19 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
爱飞 该用户已被删除
11#
发表于 2015-3-20 12:55:11 | 只看该作者
我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:
若相依 该用户已被删除
12#
 楼主| 发表于 2015-3-27 14:18:54 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
兰色精灵 该用户已被删除
13#
发表于 2015-3-28 17:39:07 | 只看该作者
建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。
蒙在股里 该用户已被删除
14#
发表于 2015-3-30 15:49:28 | 只看该作者
首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。
灵魂腐蚀 该用户已被删除
15#
发表于 2015-3-31 11:35:12 | 只看该作者
装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。
第二个灵魂 该用户已被删除
16#
发表于 2015-3-31 16:55:59 | 只看该作者
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
飘飘悠悠 该用户已被删除
17#
发表于 2015-4-4 12:27:42 | 只看该作者
做为1门年轻的语言,php一直很努力。
若天明 该用户已被删除
18#
发表于 2015-4-6 09:02:59 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
小妖女 该用户已被删除
19#
发表于 2015-4-6 09:50:37 | 只看该作者
当然这种网站的会员费就几十块钱。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-18 05:22

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表