欢迎访问WDPHP素材源码!今天是2024年04月30日 星期二,熬夜对身体不好,早点休息吧!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > PHP教程 > 

PHP实现使用txt文本作为数据库 php文本数据库操类
栏目分类:PHP教程    发布日期:2023-04-09    浏览次数:561次     收藏

php文本数据库操类

 <?php

 /**
 * 文本数据库类类,用于将txt文本文件作为数据库,实现增删改查功能
 * 源码下载:   http://www.wdphp.com
 * Author: 缘境 (yvsm@163.com) QQ:316430983 
*/

class TextDatabase {
  private $filename;

  public function __construct($filename) {
    $this->filename = $filename;
  }

  // 定义一个名为insert的方法,接受一个数据数组并将其附加到文本文件的末尾
  public function insert($data) {
    // 以追加模式打开文件
    $handle = fopen($this->filename, 'a');
    fwrite($handle, implode(',', $data) . "\n");
    fclose($handle);
  }

  // 定义一个名为selectAll的方法,返回文本文件中所有数据的数组
  public function selectAll() {
    // 以读取模式打开文件
    $handle = fopen($this->filename, 'r');
    $data = array();

    // 逐行循环读取文件
    while (!feof($handle)) {
      $line = fgets($handle);
      if (!empty($line)) {
        $data[] = explode(',', $line);
      }
    }

    fclose($handle);
    return $data;
  }

  // 定义一个名为update的方法,接受一个数据数组并更新文本文件中相应的记录
  public function update($data) {
    // 以读取模式打开文件
    $handle = fopen($this->filename, 'r');
    $updatedData = array();

    while (!feof($handle)) {
      $line = fgets($handle);

      if (!empty($line)) {
        $record = explode(',', $line);
        if ($record[0] == $data[0]) {
          $updatedData[] = implode(',', $data) . "\n";
        } else {
          $updatedData[] = $line;
        }
      }
    }
    fclose($handle);
    $handle = fopen($this->filename, 'w');
    fwrite($handle, implode('', $updatedData));
    fclose($handle);
  }

  // 定义一个名为delete的方法,接受一个ID并从文本文件中删除相应的记录
  public function delete($id) {
    $handle = fopen($this->filename, 'r');
    $updatedData = array();

    while (!feof($handle)) {
      $line = fgets($handle);

      if (!empty($line)) {
        $record = explode(',', $line);
        if ($record[0] != $id) {
          $updatedData[] = $line;
        }
      }
    }

    fclose($handle);
    $handle = fopen($this->filename, 'w');
    fwrite($handle, implode('', $updatedData));

    fclose($handle);
  }


    // 定义一个名为select的方法,接受一个ID并返回文本文件中相应的记录
public function select($id) {
      // 以读取模式打开文件
      $handle = fopen($this->filename, 'r');
  
      // 逐行循环读取文件
      while (!feof($handle)) {
        // 从文件中读取一行
        $line = fgets($handle);
  
        // 如果行不为空,则检查它是否与要查询的ID匹配
        if (!empty($line)) {
          $record = explode(',', $line);
  
          // 如果记录与要查询的ID匹配,则返回该记录
          if ($record[0] == $id) {
            // 关闭文件
            fclose($handle);
  
            // 返回记录
            return $record;
          }
        }
      }
      fclose($handle);
  }
}

使用示例

// 使用文件名"data.txt"创建TextDatabase类的新实例
$textDatabase = new TextDatabase('data.txt');

// 将新记录插入文本文件
$textDatabase->insert(array(1, 'John Doe', 'johndoe@wdphp.com'));

$find = $textDatabase->select(1);
print_r($find);
// 更新文本文件中的现有记录
$textDatabase->update(array(1, 'wdphp', 'yvsm@wdphp.com'));

// 从文本文件中删除记录
$textDatabase->delete(1);

// 选择文本文件中的所有记录
$data = $textDatabase->selectAll();

// 打印数据
print_r($data);

后语

使用文本数据库并不是一个很好的选择,或许一些特殊环境下可以用到吧~~
MySQL、SQLite、MongoDB、PgSQL都是比较好的选择

声明:本文为原创文章,如需转载,请注明来源 WDPHP.COM 并保留原文链接:https://www.wdphp.com/detail/2007.html

相关热词: 文本数据库txt数据库

源码 模板 特效 素材 资源 教程 站长