Use dbm as a database in PHP (including sorting)

Source: Internet
Author: User
Tags date array functions join key mysql php database sort
Sort | data | Database in many CGI languages, PHP with its simple, rapid advantages began to grow, the use of PHP development program more and more people, and the general PHP database on two types: text and MySQL. Text database reading, writing speed is slow, when the data reaches a certain amount will greatly reduce speed and even crash! While MySQL, although fast, powerful, because the general free space does not support MySQL, because the general free space does not support MySQL (a host of friends do not look down)
Today, the author introduces the DBM database, DBM is the development of the University of Berkeley file/text database, in the BSD system has been installed, even if not installed, in the PHP4.03 also joined the DBM support. As a result, DBM is supported in most PHP-enabled space (see www.zphp.com for PHP), and step-by-step is introduced in PHP using DBM as a database:

The way to determine if your space supports dbm:
Enter the following program:
----------------------------------------------------
?
Echo DBList ();
?>
----------------------------------------------------
Save As dbmtest.php, run to see if the output function is undefined, if not, congratulations ...

Second, the basic functions of PHP using dbm:
1, int dbmopen (string filepath,string mode);
This can open a dbm database, where filepath is the path to the DBM database, mode has 4 parameters: "R" opens the database in read-only mode, "W" opens the database in read-write mode, and "C" opens the database in read-write mode, if it does not exist; "n" Deletes the existing database, Open the database in read-write mode.
2, Boolean dbmclose (int handle); Closes an already-opened dbm database while releasing handle.
3, String dbmfetch (int handle,string key); Gets the value of the key for the handle database that has been opened.
4, Boolean dbmexists (int handle, string key); Determines whether a key exists in the handle database that is already open.
5, string dbmfirstkey (int handle); Gets the physical first key of the handle database that has been opened.
6, String dbmnextkey (int handle,string key);
Obtain the next key for the key in the handle database that has been opened (that is, the Dbmnextkey and Dbmfirstkey two functions implement the traversal search in dbm!). )
7, Boolean dbminsert (int handle,string key,string value);
Inserts a key in the handle database that is already open, and its corresponding value, and returns False if the key already exists.
8. Boolean dbmreplace (int handle, string key, String value);
Replace the value of the key in the handle database that you have already opened to become value, or return the build if no key exists.
9, Boolean dbmdelete (int handle,string key), delete key in the handle database that has already been opened.


Iii. Considerations for using dbm:
1, dbm Database Unlike SQL, it is only a simple key/value positioning, if you want to store a variety of information, you can only use a separator to separate information, as follows (here with "|!:!|" Do separator)
name|!:!| telno|!:!| Mailadd//The name, telephone and mailbox are stored separately
The read-time method is as follows:
----------------------------------------------------
$data =explode (' |!:!| ' Dbmfetch ($dbmid, $key));
Then $data[o] corresponds the name, $data [1] corresponds to the call, $data [2] corresponds to the mailbox
----------------------------------------------------
2, dbm itself stored data without any physical order, only through their own processing (see below) to order!
3, dbm is not like text, the transfer of a DB from this host to another host will be wrong, that is, once a DB file, you can not transfer!
4, dbm under NT a key corresponding to the length of the value can not exceed 1k characters, so the NT can not use DBM to retain some of the length of things!
5, about the use of the Chinese in dbm key:dbm key can not use Chinese, the author has tried in a total program, if the use of Chinese as key words when key one more (about 20) will appear unable to traverse the search problem!

Iv. use dbm to do the unordered database:
Using dbm to do the unordered database (that is, the data without order concept) is very simple, much simpler than the text database! For example, the following is a program that lets the user give a user a call after entering a user name:
----------------------------------------------------
?
if (Isset ($userid)) {
$data =dbmopen ("path", "R");
if (Dbmexists ($data, $userid)) echo $no =dbmfetch ($data, $userid);
else echo "UserID error!";
Dbmclose ($data);
}else{
?>
Please enter your username:
<form action=<?echo$php_self;? >
<input type= "text" name= "userid" >
<input type= "Submit" name= "Submit" &GT;&LT;/FORM&GT;&LT; >
----------------------------------------------------

V. To do an orderly database with dbm:
Because DBM does not sort the data, it is tricky to have programs that are output in a certain order (for example, just to show all users ' phones), which requires a human sort, and the following author gives two methods:
1, sorting Data method: In this method we specialize in a key to the corresponding value to record the order, that key we artificially named Sort,sort corresponding values are as follows:
Data1 ' s key|data2 ' s key|data3 ' s key|........| Data n ' s key
Where the data n ' s dey length needs to be certain (the author uses the time method, the following program can generate key:)
----------------------------------------------------
function Getkey () {
$date =date ("Ymdhis");
return $date;
}
----------------------------------------------------
With Getkey () you get a 12-bit key like 001203114950, and each key corresponds to its own value (the user's phone here), and substr ($sort, $i *13,12) can read the key of the first user, The following is the code that displays the list:
----------------------------------------------------
?
First to know how many users, you can write a special num to record
$data =dbmopen ("path", "R"); Open Database
$sort =dbmfetch ($data, "sort"); Reading sort
for ($i =0; $i < $totaluser; $i + +) {
$key =substr ($sort, $i *13,12); Ann Order to get key
$telno =dbmfetch ($data, $key); Read the value of key corresponding
echo $i +1. " User ' s Telno is '. $telno. " <br> ";
}
Dbmclose ($data);
?>
----------------------------------------------------

2, Array sorting method: Thank you here, no brother, is he let the author think of the array to sort. The basic principle of array ordering is to read each key of the DBM database into an array and then sort and output it using functions such as Usort () based on the size of each key.
Because it is the size of the key to sort, so the length of the key does not have any restrictions, as long as the key added after the guarantee is greater than the first to join the first display after the person to join, here with time () as a key.
Here is the list code:
----------------------------------------------------
$data =dbmopen ("path", "R"); Open Database
$i = 1;
For ($key =dbmfirstkey ($data); $key $key =dbmnextkey ($data, $key)) {
$sort [$i]= $key;
$i + +;
}//Traverse to get all key
Usort ($sort); Ann Size Sort
for ($i =0; $i <count ($sort); $i + +)
echo $i +1. " User ' s Telno is '. Dbmfetch ($data, $sort [$i]). " <br> ";
Dbmclose ($data);
----------------------------------------------------
Both of these methods occupy memory at run time (for sorting) the first is a little bigger than the second, however, for the calculation, the first method is less than the second, as to what kind of method you want to use (if you are interested, you can try both the CPU occupancy rate).

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.