/**
* 文件数据库函数
* 约定:
* 数据库名为同名目录
* 数据表名后缀为 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]." | ";
?>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software