一组文件数据库函数_PHP
/**
* 文件数据库函数
* 约定:
* 数据库名为同名目录
* 数据表名后缀为 tab
* 索引文件后缀为 ind
* 管理文件名为 数据库同名目录/manage.ini
* 备注型字段以独立文件存在,表中保留文件名
* 字段以定长方式或csv方式保存,由管理文件指定
* 每行一条记录
* 索引文件以二进制方式包存键值(定长)和偏移量(长整型)
*
** 函数列表(前面有:+ 为已实现,- 为部分实现,其余为尚未实现)
套用 MySQL 函数集 取名 FILE_DB
* file_affected_rows: 得到 FILE_DB 最后操作影响的列数目。
*-file_close: 关闭 FILE_DB 服务器连接。
* file_connect: 打开 FILE_DB 服务器连接。
*+file_create_db: 建立一个 FILE_DB 新数据库。
* file_data_seek: 移动内部返回指针。
* file_db_query: 送查询字符串 (query) 到 FILE_DB 数据库。
* file_drop_db: 移除数据库。
* file_errno: 返回错误信息代码。
* file_error: 返回错误信息。
*+file_fetch_array: 返回数组资料。
* file_fetch_field: 取得字段信息。
* file_fetch_lengths: 返回单列各栏资料最大长度。
* file_fetch_object: 返回类资料。
*+file_fetch_row: 返回单列的各字段。
*+file_field_name: 返回指定字段的名称。
* file_field_seek: 配置指针到返回值的某字段。
* file_field_table: 获得目前字段的资料表 (table) 名称。
* file_field_type: 获得目前字段的类型。
* file_field_flags: 获得目前字段的标志。
* file_field_len: 获得目前字段的长度。
* file_free_result: 释放返回占用内存。
* file_insert_id: 返回最后一次使用 INSERT 指令的 ID。
* file_list_fields: 列出指定资料表的字段 (field)。
* file_list_dbs: 列出 FILE_DB 服务器可用的数据库 (database)。
* file_list_tables: 列出指定数据库的资料表 (table)。
*+file_num_fields: 取得返回字段的数目。
*+file_num_rows: 取得返回列的数目。
* file_pconnect: 打开 FILE_DB 服务器持续连接。
*-file_query: 送出一个 query 字符串。
* file_result: 取得查询 (query) 的结果。
* file_select_db: 选择一个数据库。
* file_tablename: 取得资料表名称。
*/
/**
*
*
$cmp_key;
/** 连结至 FILE_DB 资料库
* @param 数据库名
*/
function &file_connect($dbname) {
$filename = $dbname."/manage.ini";
if(!file_exists($filename))
die("数据库:$dbnamee不存在");
$fp = fopen($filename,"r");
$s = fgets($fp,filesize($filename)+1);
fclose($fp);
$db = unserialize($s);
return $db;
}
/** 关闭 FILE_DB 连结
*
*/
function file_close(&$connection_id) {
unset($connection_id);
}
/** 建立 FILE_DB 数据库
* @param 数据库名
*/
function file_create_db($dbname) {
if(! @mkdir($dbname,0700))
$err = "目录已存在";
$filename = $dbname."/manage.ini";
$ar[path] = "$dbname/";
$s = serialize($ar);
$fp = fopen($filename,"w");
fputs($fp,$s);
fclose($fp);
}
/** 执行 SQL 指令
*
*/
function file_query($query,&$connection_id,$_line = __LINE__) {
// 解析 $query
$query = eregi_replace("[ ]+"," ",$query);
$ar = split(" ",trim(str_replace(" "," ",$query)));
$query = eregi_replace("\r?\n","",$query);
$ch = each($ar);
switch(strtoupper($ch[1])) {
case "CREATE":
if(! eregi("create table (.+) [(] *(.+)[)] *$",$query,$pp))
die("SQL表达式错,$_line");
$table = $pp[1];
if(empty($connection_id[$table])) {
// 解析字段表达式
$ar = split(",",$pp[2]);
while(list($key,$value) = each($ar)) {
$str = split(" ",trim($value));
eregi("([a-z0-9]+) ([a-z]+) ?([(]([0-9]+)[)])?(.+)?",$value,$pp);
$field[name] = $pp[1];
$field[type] = $pp[2];
$field[len] = $pp[4];
$field[style] = trim($pp[5]);
$sss[] = $field;
}
$connection_id[$table] = array(
"fields" => $sss,
"filename" => $table.".tab",
"type" => "CSV"
);
}
$fp = fopen($connection_id[path]."manage.ini","w");
fputs($fp,serialize($connection_id));
fclose($fp);
break;
case "SELECT":
if(! eregi("select (.+) from ([^ ]+) *(.+)*$",$query,$pp))
die("SQL表达式错,$_line");
// 解析SQL语句的控制部分
$exte = $pp[3];
if(! eregi("where ",$exte))
$exte = "where true ".$exte;
if(! eregi("order ",$exte))
$exte .= "order by 0";
if(! eregi("group ",$exte))
$exte = eregi_replace("order ","group by _ order ",$exte);
eregi("where (.+) *group by (.+) order by (.+)",$exte,$regs);
$filter = trim($regs[1]);
$group = trim($regs[2]);
$order = trim($regs[3]);
if($group == "_") $group = "";
if($order == "0") $order = "";
// 读取数据文件
if(empty($connection_id[$pp[2]]))
die("表".$pp[2]."不存在");
$table = $connection_id[$pp[2]];
if(!file_exists($connection_id[path].$table[filename])) {
// 数据表尚未建立
return false;
}else {
// 获取表定义字段名
for($i=0;$i
$defaultfield = array_flip($defaultfield);
// 解析筛选表达式
if($filter != "true") {
$filter = eregi_replace(" and "," && ",$filter);
$filter = eregi_replace(" or "," || ",$filter);
$filter = eregi_replace("=","==",$filter);
$filter = eregi_replace("==>","=>",$filter);
$filter = eregi_replace(" $filter = eregi_replace("===","==",$filter);
while (ereg ('([a-z][a-z0-9_]*)', $filter, $regs)) {
$found = $regs[1];
$filter = eregi_replace("$found","[". $defaultfield][$found]."]", $filter);
}
$filter = eregi_replace("\[","\$tmp][",$filter);
$filter = "\$key=($filter);";
}else
$filter ="";
// 装入数据
$fp = fopen($connection_id][path].$table[filename],"r");
$key = true;
while($tmp = fgetcsv($fp,4096,"`")) {
if(!empty($filter)) eval($filter);
if($key)
$temp[] = $tmp;
}
fclose($fp);
}
// 解析输出字段
$f = split(",",$pp[1]);
$expr = "";
while(list($key,$value) = each($f)) {
if(eregi("(.+)[(](.+)[)]( as (.+))?",$value,$pp)) {
// 有函数
switch(strtoupper($pp[1])) {
case "COUNT":
if(empty($pp[4]))
$n = "cnt";
else
$n = $pp[4];
$expr .= "\$rs[$n]=1;";
$sumname[] = $n;
break;
}
}else if(eregi("(.+) as (.+)",$value,$pp)) {
// 有重命名
$n = $defaultfield[$pp[1]];
if(!isset($n)) die("字段名 $n 非法");
$expr .= "\$rs[$pp[2]]=\$temp[\$i][$n];";
}else if($value == "*") {
for($i=0;$i
$expr .= "\$rs[$value]=\$temp[\$i][$i];";
}
}else {
$n = $defaultfield[$value];
if(!isset($n)) die("SQL error");
$expr .= "\$rs[$value]=\$temp[\$i][$n];";
}
}
// 取得输出字段列表
$i=0;
eval($expr);
$outfield = array_keys($rs);
// 解析分组
if($group) {
$groups = split(",",$group);
for($i=0;$i
$n = $groups[$i]>0?$groups[$i]-1:0;
$groups[$i] = $outfield[$n];
}
}
// 过录数据
for($i=0;$i
if(!$group)
$result[] = $rs;
else {
$n = $rs[$groups[0]];
if($sumname) {
for($ii=0;$ii
$result[$n] = $rs;
for($ii=0;$ii
}else
$result[$n] = $rs;
}
}
unset($temp);
// 解析排序
if($order) {
global $cmp_key;
$orders = split(",",$order);
for($i=count($orders)-1;$i>=0;$i--) {
eregi("([0-9]+)?([a-z_][a-z0-9_]*)? *(desc)?",$orders[$i],$regs);
if(!empty($regs[1])) {
$n = $regs[1]-1;
if($n $cmp_key = $outfield[$n];
}else
$cmp_key = $regs[2];
if(empty($regs[3]))
usort($result, cmp_asc);
else
usort($result, cmp_desc);
}
}
return $result;
break;
case "INSERT":
if(! eregi("insert into table (.+) [(](.+)[)] *values? *[(](.+)[)]",$query,$pp))
die("SQL表达式错,$_line");
if(empty($connection_id[$pp[1]]))
die("表".$pp[1]."不存在");
$table = $connection_id[$pp[1]];
// 解析目标字段名
$f = split(",",$pp[2]);
// 检查字段是否合法
for($i=0;$i
die("字段名非法 ".$f[$i]);
// 根据表字段表构造数据数组
for($i=0;$i
// 解析数据
eval("\$d=array($pp[3]);");
// 填表
for($i=0;$i
}
$fp = fopen($connection_id[path].$table[filename],"a");
fputs($fp,join("`",$data)."\n");
fclose($fp);
break;
case "UPDATE":
break;
default:
die("错误的SQL指令,在".$_line."行");
}
}
/** file_fetch_row: 返回单列的各字段
*
*/
function file_fetch_row(&$result) {
$ar = each($result);
if(! $ar) return false;
while(list($k,$ch) = each($ar[1]))
$br[] = $ch;
return $br;
}
/** file_fetch_array: 返回数组资料
*
*/
function file_fetch_array(&$result) {
$ar = each($result);
if(! $ar) return false;
while(list($k,$ch) = each($ar[1])) {
$br[] = $ch;
$br[$k] = $ch;
}
return $br;
}
/** file_field_name: 返回指定字段的名称
*
*/
function file_field_name(&$result,$n=0) {
$outfield = array_keys($result[0]);
return $outfield[$n];
}
/** file_num_rows: 取得返回列的数目
*
*/
function file_num_rows(&$result) {
return count($result);
}
/** file_num_fields: 取得返回字段的数目
*
*/
function file_num_fields(&$result) {
return count($result[0]);
}
/** 工作函数集
*/
/** 在数组中检索
* @param $ar 数组
* @param $val 待检索的值
* @param $key 键
*/
function array_scan($ar,$val,$key=0) {
for($i=0;$i
return true;
return false;
}
/** 排序用工作函数(降序 由usort()调用)
*/
function cmp_desc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? -1 : 1;
}
/** 排序用工作函数(升序 由usort()调用)
*/
function cmp_asc($a,$b) {
global $cmp_key;
if ($a[$cmp_key] == $b[$cmp_key]) return 0;
return ($a[$cmp_key] > $b[$cmp_key]) ? 1 : -1;
}
// 应用实例
file_create_db("test");
$conn = file_connect("test");
$s = "create table board (
sn int primary key auto_increment,
name varchar(15) not null,
email varchar(30),
ip varchar(15),
time datetime,
content tinyblob not null)";
file_query($s,$conn);
//file_query("insert into table board (sn,name,ip) value (15,'w,r,r,oi','$REMOTE_ADDR')",$conn);
//$rs = file_query("select * from board",$conn);
$rs = file_query("select * from board where true group by 2 order by 2 desc",$conn);
//$rs = file_query("select count(*) as js,time as hj,ip as pi,name,sn from board group by name order by sn desc",$conn);
//$rs = file_query("select sn,name,email,io from board where true group by 1 order by 2 desc",$conn);
file_close($conn);
echo "
".file_field_name($rs,$i)." | ";
---|
".$d[$i]." | ";
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Open WeChat, select Settings in Me, select General and then select Storage Space, select Management in Storage Space, select the conversation in which you want to restore files and select the exclamation mark icon. Tutorial Applicable Model: iPhone13 System: iOS15.3 Version: WeChat 8.0.24 Analysis 1 First open WeChat and click the Settings option on the My page. 2 Then find and click General Options on the settings page. 3Then click Storage Space on the general page. 4 Next, click Manage on the storage space page. 5Finally, select the conversation in which you want to recover files and click the exclamation mark icon on the right. Supplement: WeChat files generally expire in a few days. If the file received by WeChat has not been clicked, the WeChat system will clear it after 72 hours. If the WeChat file has been viewed,

In Windows, the Photos app is a convenient way to view and manage photos and videos. Through this application, users can easily access their multimedia files without installing additional software. However, sometimes users may encounter some problems, such as encountering a "This file cannot be opened because the format is not supported" error message when using the Photos app, or file corruption when trying to open photos or videos. This situation can be confusing and inconvenient for users, requiring some investigation and fixes to resolve the issues. Users see the following error when they try to open photos or videos on the Photos app. Sorry, Photos cannot open this file because the format is not currently supported, or the file

In this article, we will introduce how to solve the problem of "Ready to delete" prompt when deleting files or folders in Windows system. This prompt means that the system is performing some background operations, such as checking file permissions, verifying whether the file is occupied by other programs, calculating the size of the item to be deleted, etc. We will provide you with some workarounds to ensure that you can successfully delete your files without waiting too long. Why does Windows take so long to delete files? The time it takes Windows to prepare a file for deletion is affected by a variety of factors, including file size, storage device speed, and background processes. A long or stuck "Preparing to delete" prompt may indicate insufficient system resources, disk errors, or file system issues. exist

Tmp format files are a temporary file format usually generated by a computer system or program during execution. The purpose of these files is to store temporary data to help the program run properly or improve performance. Once the program execution is completed or the computer is restarted, these tmp files are often no longer necessary. Therefore, for Tmp format files, they are essentially deletable. Moreover, deleting these tmp files can free up hard disk space and ensure the normal operation of the computer. However, before deleting Tmp format files, we need to

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

The gho file is a GhostImage image file, which is usually used to back up the entire hard disk or partition data into a file. In some specific cases, we need to reinstall this gho file back to the hard drive to restore the hard drive or partition to its previous state. The following will introduce how to install the gho file. First, before installation, we need to prepare the following tools and materials: Entity gho file: Make sure you have a complete gho file, which usually has a .gho suffix and contains a backup

In C language, if statement is usually used to execute a specific block of code based on a single condition. However, multiple conditions can be combined to make a determination using logical operators such as &&, ||, and !. Including using logical AND (&&) to judge multiple conditions, using logical OR (||) to judge at least one condition, using logical NOT (!) to judge the negation of a single condition, as well as nesting if statements and using parentheses to clarify priority.

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it
