一组文件数据库函数
函数|数据|数据库
/**
* 文件数据库函数
* 约定:
* 数据库名为同名目录
* 数据表名后缀为 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

AI Hentai Generator
Generate AI Hentai for free.

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

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode
