电脑安全

您当前的位置:主页 > 电脑安全 >

自己动手做一个SQL解释器-网络编程

来源:[db:来源]标题:   这是    编辑:电脑技术全能网 时间:2019-09-20 18:31

原标题:自己动手做一个SQL解释器-网络编程
   这是从其余处所看到的,俺还不会写这么无聊的货色<?phpclass DB_text {var $conn;var $classname = "db_text";var $database;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);if(empty($type)) $type = "asc";$this->_sort($rs,$key,$type);}return $rs;

上一篇:PHP开发环境安装包-网络编程

下一篇:没有了