DBA extended learning of PHP


The DBA we are talking about today is not the traditional DBA of database administrator, but an extension of Buckley style database in PHP. Buckley style database is actually a K / V database in the form of key value pairs. Like memcached or redis, which we usually use a lot, only a key corresponds to a value. However, memcached is mainly stored in memory, while DBA extension stores data in files, just like a simple SQLite in the form of key value pairs.

The database types used by DBA extensions are basically open source. The deployment and release are very simple. It is a DB file, so it is very similar to SQLite. However, the disadvantage is that it will load the database file into memory at one time. We can’t make the database too large, otherwise it will burst the memory. DBA databases are all together with programs, so it has no network related interfaces. Generally, we only use this database locally.

During installation, we need to add — enable DBA = shared configuration during compilation, and then add a — enable XXXX configuration. XXXX refers to the type of Buckley style database we want to use. The more common ones are DBM, ndbm, gdbm, DB2, etc. Similarly, the operating system also needs to install these related software. For example, gdbm is installed in our system, which needs to be installed using Yum install.

A simple example

First, let’s look at how our DBA database is used through the code

//Open a database file
$id = dba_open("/tmp/test.db", "n", "gdbm");
//$id = dba_popen("/tmp/test1.db", "c", "gdbm");

//Add or replace a content
dba_replace("key1", "This is an example!", $id);

//If content exists
if(dba_exists("key1", $id)){
    //Read content
    echo dba_fetch("key1", $id), PHP_EOL;
    // This is an example!


The first is to use DBA_ Open() to open a database file. The first parameter is the path of the database file. The second parameter is the opening method, including R, W, C and N. R means read-only, w means read-write, C means create plus read-write, and N means create and read-write if not. The third parameter is the specified database type. Only gdbm library is installed in our system, so we use gdbm as the parameter. Like mysql, we can also use DBA_ Popen () to open a persistent link to a data file.

dba_ The replace() function adds or replaces a piece of data. If the data does not exist, it adds a new one. If it does exist, it replaces the value of the corresponding key. The first parameter is the key, and the second parameter is the data value.

dba_ Exists () is to judge whether the specified key exists. If so, we will pass DBA in this if_ Fetch() gets the data specified by the key.

dba_ Close () is the same as other data operation handles, closing the connection handle of the database.

Add, traverse and delete data

In the above example, we use DBA_ Replace() to add data. In fact, formal data addition has special functions.

//Add data
dba_insert("key2","This is key2!", $id);
dba_insert("key3","This is key3!", $id);
dba_insert("key4","This is key4!", $id);
dba_insert("key5","This is key5!", $id);
dba_insert("key6","This is key6!", $id);

//Get the first key
$key = dba_firstkey($id);

$handle_later = [];
while ($key !== false) {
    if (true) {
        //Save key to array
        $handle_later[] = $key;
    //Get next key
    $key = dba_nextkey($id);

//Traverse the key array and print all the contents in the database
foreach ($handle_later as $val) {
    echo dba_fetch($val, $id), PHP_EOL;
    dba_ delete($val, $id); //  Delete the content corresponding to the key
// This is key4!
// This is key2!
// This is key3!
// This is an example!
// This is key5!
// This is key6!

dba_ Insert () is to insert data. It will not replace the existing key data. If it is to insert the existing key information, it will return false.

dba_ The first key () is used to get the first key, DBA_ Nextkey () is used to get the next key. Through these two functions, we can get all the key information in the whole database, and then we can traverse all the contents in the whole database through these keys.

dba_ Delete () deletes a piece of data according to the key.

Optimize and synchronize database

Even if MySQL is used for a long time, we also need to do some sorting and optimization work, such as making MySQL automatically sort out file fragments and indexes. The SQL statement used is: optimize table name. Similarly, the DBA extension also provides us with such a function.

//Optimize database
var_dump(dba_optimize($id)); // bool(true)

In addition, just like MySQL cache, DBA will cache data when operating. At this time, we can use a function to force the data in the cache into the hard disk file.

//Synchronize database
var_dump(dba_sync($id)); // bool(true)

List of currently open databases

We can use a function to check which data connections are currently open. Because DBA is a simple file based database, we can open multiple data connections in a piece of code.

//Gets the list of currently open databases
// array(1) {
//     [4]=>
//     string(12) "/tmp/test.db"
//   }

Database types supported by the system

Finally, let’s look at a supported function, which can return the database types currently supported by our database.

//Currently supported database types
// array(5) {
//     ["gdbm"]=>
//     string(58) "GDBM version 1.18. 21/08/2018 (built May 11 2019 01:10:11)"
//     ["cdb"]=>
//     string(53) "0.75, $Id: 841505a20a8c9c8e35cac5b5dc3d5cf2fe917478 $"
//     ["cdb_make"]=>
//     string(53) "0.75, $Id: 95b1c2518144e0151afa6b2b8c7bf31bf1f037ed $"
//     ["inifile"]=>
//     string(52) "1.0, $Id: 42cb3bb7617b5744add2ab117b45b3a1e37e7175 $"
//     ["flatfile"]=>
//     string(52) "1.0, $Id: 410f3405266f41bafffc8993929b8830b761436b $"
//   }

// array(5) {
//     [0]=>
//     string(4) "gdbm"
//     [1]=>
//     string(3) "cdb"
//     [2]=>
//     string(8) "cdb_make"
//     [3]=>
//     string(7) "inifile"
//     [4]=>
//     string(8) "flatfile"
//   }

dba_ Handlers () has a Boolean parameter. From the code, we can see that the function of this parameter is to return the details of the information.


Today’s introduction is a very simple set of database extension components. Its functions are these. In the daily production environment, there are not many actual application scenarios. We can use PHP file serialization to save simple key value pairs, and memcached and other tools will be used for caching, so let’s have a look.

Test code:


Reference documents:



Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]