Detailed introduction to PHP DBA extension
This article will introduce you to the DBA extension of PHP in detail. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Extended DBA learning in PHP
The DBA we are talking about today is not the traditional database administrator DBA, but a Buckley style in PHP Database extensions. The Buckley style database is actually what we often call a K/V database in the form of key-value pairs. Just like memcached or redis, which we usually use a lot, only one key corresponds to one value, but memcached mainly stores them in memory, while DBA extension stores data in files, just like a simple key Same as SQLite in value pair form.
The database types used by DBA extensions are basically open source, and deployment and release are very simple. It is just a db file, so it is very similar to SQLite. But the disadvantage is that it will load the database file into the memory at once. We cannot make the database too large, otherwise it will burst the memory. The DBA database is always together with the program, so it does not have network-related interfaces. We generally only use this kind of database locally in the code.
When installing, we need to add the --enable-dba=shared configuration during compilation, and then add an --enable-XXXX configuration. XXXX refers to the Buckley style database we want to use. Types, the more common ones include dbm, ndbm, gdbm, db2, etc. Similarly, the operating system also needs to install these related software. For example, our system is installed with gdbm, which needs to be installed using yum install.
A simple example
First let’s look at the code to see how our DBA database is used.
// 打开一个数据库文件 $id = dba_open("/tmp/test.db", "n", "gdbm"); //$id = dba_popen("/tmp/test1.db", "c", "gdbm"); // 添加或替换一个内容 dba_replace("key1", "This is an example!", $id); // 如果内容存在 if(dba_exists("key1", $id)){ // 读取内容 echo dba_fetch("key1", $id), PHP_EOL; // This is an example! } dba_close($id);
First, 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, n, r means read-only, w means read-write, c means create plus read-write, n means if not It is created and can be read and written. The third parameter is the specified database type. Only the 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_replace() function is to add or replace a piece of data. If the data does not exist, add a new one. If the data exists, replace the value of the corresponding key. The first parameter is key, and the second parameter is the value of the data.
dba_exists() is to determine whether the specified key exists. If it exists, we will obtain the data specified by the key through dba_fetch() in this if.
dba_close() is just like other data operation handles, it closes the database connection handle.
Add, traverse, and delete data
In the above example, we use dba_replace() to add data. In fact, there are special functions for regular data addition.
// 添加数据 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); // 获取第一个 key $key = dba_firstkey($id); $handle_later = []; while ($key !== false) { if (true) { // 将 key 保存到数组中 $handle_later[] = $key; } // 获取下一个 key $key = dba_nextkey($id); } // 遍历 key 数组,打印数据库中的全部内容 foreach ($handle_later as $val) { echo dba_fetch($val, $id), PHP_EOL; dba_delete($val, $id); // 删除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_firstkey() 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 entire database, and then we can These keys are used to traverse everything in the entire database.
dba_delete() deletes a piece of data based on the key.
Optimization and synchronization database
Even if it is mysql, after long-term use, we also need to do some sorting and optimization work, such as letting mysql automatically defragment files, organize indexes, etc., it uses The SQL statement is: optimize table name. In the same way, DBA extension also provides us with such a function.
// 优化数据库 var_dump(dba_optimize($id)); // bool(true)
In addition, just like the cache of mysql, the DBA will also cache when operating data. At this time, we can use a function to force the data in the cache to be flushed into the hard disk file.
// 同步数据库 var_dump(dba_sync($id)); // bool(true)
Currently open database list
We can use a function to see which data connections are currently open, because DBA is a simple file-based database, so we can open it in a piece of code Multiple data connections.
// 获取当前打开的数据库列表 var_dump(dba_list()); // 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.
// 当前支持的数据库类型 var_dump(dba_handlers(true)); // 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 $" // } var_dump(dba_handlers(false)); // 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 the detailed level of the returned information.
Summary
What we introduced today is a very simple set of database extension components. Its functions are just these. In daily production environments, there are not many actual application scenarios. We can use PHP file serialization to save simple key-value pairs, while caching will mostly use tools such as memcached, so you can learn about it.
Test code:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202008/PHP%E7%9A%84DBA%E6%89%A9%E5%B1%95%E5%AD%A6%E4%B9%A0.md
Recommended learning: php video tutorial
The above is the detailed content of Detailed introduction to PHP DBA extension. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
