Do a SQL Interpreter yourself
In some small applications, there is no need to use large database software at all. Making a SQL interpreter of your own can be managed in a database way.
This interpreter explains the commonly used SQL commands. You can add other features yourself.
<?php
Class 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);
}
while (the list ($key, $value) = each ($t)) {
eval ($expr);
if ($EXPL)
$keys [] = $key;
}
return $keys;
}
Function _sort (& $ar, $key =0, $mode = "desc") {
Global $cmp _key;
$cmp _key = $key;
if ($mode = = "ASC")
Usort ($ar, _CMP_ASC);
Else
Usort ($ar, _cmp_desc);
}
function _close () {
$fp = fopen ($this->database, "w");
Fwrite ($FP, serialize ($this->conn));
Fclose ($FP);
}
}
/** sort key
*/
$cmp _key = "";
/** sort with work functions (descending by 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;
}
/** sorting with work functions (ascending is called by 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;
}
?>
Test Example:
<pre>
<?php
Require_once "db_text.php";
$conn = new DB_Text;
$conn->connect ("Text1.txt");
$conn->query ("INSERT into Manage (Id,title) VALUES (' ABCD ')");
$conn->query ("INSERT into Manage (Id,title) VALUES (2, ' 43d ')");
$conn->query ("INSERT into Manage (Id,title) VALUES (' Tuu ')");
$conn->query ("Update manage set id=101,test= ' 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-id desc");
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.