Table of Contents
Extended DBA learning in PHP
A simple example
Add, traverse, and delete data
Optimization and synchronization database
Currently open database list
Database types supported by the system
Summary
Home Backend Development PHP Problem Detailed introduction to PHP DBA extension

Detailed introduction to PHP DBA extension

Jun 10, 2021 pm 04:59 PM
php

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.

Detailed introduction to PHP DBA extension

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);
Copy after login

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!
Copy after login

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)
Copy after login

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)
Copy after login

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"
//   }
Copy after login

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"
//   }
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles