代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
SQL的形式操作文件
作者:
李敬塔
/ 发布于
2012/12/5
/
677
<?php /**** **createDate:2009-01-11 **author:李敬塔 *****/ class DB_text { var $conn; var $classname = "db_text"; var $database; www phperz com function on_create() { } function connect($database_name) { $this->database = $database_name; if(! file_exists($database_name)) { $this->conn = array(); $this->_close(); } $fp = fopen($this->database,"r"); $this->conn = unserialize(fread($fp,filesize($this->database))); fclose($fp); } function &query($query) { if(eregi("select ",$query)) return $this->_select($query); if(eregi("insert ",$query)) return $this->_insert($query); if(eregi("delete ",$query)) return $this->_delete($query); if(eregi("update ",$query)) return $this->_update($query); return array(); } function fetch_row(&$result) { if(list($key,$value) = each($result)) return $value; return false; } function num_rows($result) { return count($result); } /** * query的辅助函数 */ function _select($query) { if(eregi("(order by (.+))",$query,$regs)) { $order = $regs[2]; $query = eregi_replace($regs[1],"",$query); } if(eregi("(group by (.+))",$query,$regs)) { $group = $regs[2]; $query = eregi_replace($regs[1],"",$query); } eregi("select .* from ([0-9a-z_]+) *(where +(.+))?",$query,$regs); if($regs[3] != "") { $keys = $this->_where($regs[3],"\$this->conn[$regs[1]]"); while(list($key,$value) = each($keys)) { $rs[] = $this->conn[$regs[1]][$value]; } }else { $rs = $this->conn[$regs[1]]; } if($order) { sscanf($order,"%s %s",$key,$type); phperz.com if(empty($type)) $type = "asc"; $this->_sort($rs,$key,$type); } return $rs; } function _insert($query) { eregi("insert +into +([0-9a-z_]+) *(.+) *values? *(.+)",$query,$regs); phperz.com eval("\$key=array$regs[2];"); eval("\$value=array$regs[3];"); for($i=0;$i<count($key);$i++) $rs[$key[$i]] = $value[$i]; $this->conn[$regs[1]][] = $rs; $this->_close(); } function _update($query) { eregi("update +([0-9a-z_]+) +set *(,?.*=.*)+( +where +(.+))",$query,$regs); $regs[2] = eregi_replace(",","=",$regs[2]); $v = split("=",$regs[2]); $keys = $this->_where($regs[4],"\$this->conn[$regs[1]]"); phperz.com while(list($key,$value) = each($keys)) { for($i=0;$i<count($v);$i+=2) $this->conn[$regs[1]][$value][$v[$i]] = eregi_replace("'","",$v[$i+1]); } $this->_close(); } function _delete($query) { eregi("delete +from +([0-9a-z_]+) *(where +(.+))?",$query,$regs); $keys = $this->_where($regs[3],"\$this->conn[$regs[1]]"); phperz.com while(list($key,$value) = each($keys)) { unset($this->conn[$regs[1]][$value]); } reset($this->conn[$regs[1]]); www.phperz.com while(list($key,$value) = each($this->conn[$regs[1]])) { $ch[] = $value; } $this->conn[$regs[1]] = $ch; phperz.com $this->_close(); } function _where($search,$table) { $search = eregi_replace("\("," ( ",$search); $search = eregi_replace("\)"," ) ",$search); www phperz com $search = eregi_replace("\+"," + ",$search); $search = eregi_replace("\*"," * ",$search); while(eregi("[^ ]([*/><!=-])",$search,$regs)) { $search = eregi_replace($regs[1]," $regs[1] ",$search); } while(eregi("([><!] +=)",$search,$regs)) { $search = eregi_replace($regs[1],eregi_replace(" ","",$regs[1]),$search); phperz.com } $search = eregi_replace(" "," ",trim($search)); $search = eregi_replace(" and "," && ",$search); $search = eregi_replace(" or "," || ",$search); $search = eregi_replace(" = "," == ",$search); $ar = split(" ",$search); eval("\$t=$table;"); for($i=0;$i<count($ar);$i++) { www~phperz~.com if(isset($t[0][$ar[$i]])) $ar[$i] = "\$value[".$ar][$i]."]"; } $expr = "\$expl=(".join(" ",$ar).");"; phperz.com while(list($key,$value) = each($t)) { eval($expr); if($expl) $keys[] = $key; } return $keys; } function _sort(&$ar,$key=0,$mode="desc") { phperz.com global $cmp_key; $cmp_key = $key; if($mode == "asc") usort($ar,_cmp_asc); else usort($ar,_cmp_desc); } function _close() { www~phperz~.com $fp = fopen($this->database,"w"); fwrite($fp,serialize($this->conn)); fclose($fp); } } /** 排序键 */ $cmp_key = ""; /** 排序用工作函数(降序 由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; } ?> testSQL.php 这个是测试的文件哦,分别建两个php 文件 ,copy ->Paste 就0k啦^_^ <?php //require_once "db_text.php"; $conn = new DB_text; $conn->connect("text1.txt"); $conn->query("insert into manage (id,title) values (10,'abcd')"); $conn->query("insert into manage (id,title) values (2,'43d')"); $conn->query("insert into manage (id,title) values (20,'tuu')"); $conn->query("update manage set id=101,test='a' where id=10"); //$conn->query("delete from manage where id='10'"); //$conn->query("delete from manage where id=10 or table='code'"); //$rt = $conn->query("select * from manage where id=101 or table='code' group by 1 order by 1 asc"); $rt = $conn->query("select * from manage group by 1 order by id desc"); print_r($rt); ?>
试试其它关键字
操作文件
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
李敬塔
贡献的其它代码
(
13
)
.
PHP模拟登录获取邮箱联系人地址
.
HTML节点交换(兼容FF/IE)
.
js数组分页
.
兼容FF的右键菜单
.
实现textarea内字符串选择查询替换功能
.
SQL的形式操作文件
.
可以传对像的setTimeout函数
.
音频播放器
.
仿 MSN网站首页弹出式欢迎词(兼容Firefox/IE)
.
支持IE、FF纯js 颜色选择器
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3