Make an SQL interpreter by yourself-PHP source code

Source: Internet
Author: User
Ec (2); this is from other places, and I will not write such boring things. & lt ;? PhpclassDB_text {var $ conn; var $ classname & quot; db_text & quot; var $ database; functionon_create () {} functionconnect ($ database_name) {$ this-& gt; data script ec (2); script

This is what we can see from other places. I won't write such boring things yet.


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 );
}

/**
* Auxiliary query Functions
*/
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", $ key, $ type );
If (empty ($ type) $ type = "asc ";
$ This-> _ sort ($ rs, $ key, $ type );
}
Return $ rs;

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.