在众多CGI语言中,PHP以其简单,快速的优点开始逐渐成长,使用PHP开发程序的人也越来越多,而一般PHP用的数据库就两种:文本以及MYSQL。文本数据库读、写速度慢,当数据到达一定量时就会大大的降低速度乃至崩溃!而MYSQL虽然速度快,功能强大,因为一般的免费空间都不支持MYSQL,因为一般的免费空间都不支持MYSQL(有主机的朋友就不要往下看了)
今天笔者介绍的是DBM数据库,DBM是柏克莱大学发展的文件/文本型数据库,在BSD系统中已经安装完毕,即使没有安装,在PHP4.03中也加入了DBM的支持。因此,在大部份支持PHP的空间中都支持DBM(支持PHP的空间详见www.zphp.com)下面将分步介绍在PHP使用DBM做为数据库:
一、判断你的空间是否支持DBM的方法:
输入下面的程序:
----------------------------------------------------
echo dblist();
?>
----------------------------------------------------
保存为dbmtest.php,运行,看看是否输出函数没有定义,如果不是,恭喜……
二、PHP使用DBM的基本函数:
1、int dbmopen(string filepath,string mode);
这个可以打开一个DBM数据库,其中filepath为DBM数据库的路径,mode有4个参数:"r"以只读的方式打开数据库;"w"以读写方式打开数据库;"c"以读写方式打开数据库,若不存在则建立;"n"删去现有数据库,以读写方式打开数据库。
2、boolean dbmclose(int handle); 关闭一个已经打开了的DBM数据库,同时释放handle。
3、string dbmfetch(int handle,string key); 取得已经打开了的handle数据库的key所对应的值。
4、boolean dbmexists(int handle, string key); 判断在已经打开了的handle数据库中是否存在key。
5、string dbmfirstkey(int handle); 取得已经打开了的handle数据库的物理第一个key。
6、string dbmnextkey(int handle,string key);
取得已经打开了的handle数据库中的key所对应的下一个key(就是dbmnextkey和dbmfirstkey两个函数实现了dbm的遍历搜索!)
7、boolean dbminsert(int handle,string key,string value);
在已经打开了的handle数据库中插入一个key,其对应的值为value,如果已经存在key则返回false。
8、boolean dbmreplace(int handle, string key, string value);
在已经打开了的handle数据库中替换key所对应的值成为value,如果不存在key则返回建立。
9、boolean dbmdelete(int handle,string key);在已经打开了的handle数据库中删除key。
三、使用DBM的注意事项:
1、DBM数据库不像SQL,它只有单纯的key/Value的定位,如果你想储存多种信息,只能将信息用一个分隔符来分开,如下(这里用“|!:!|”做分隔符)
Name|!:!|TelNo|!:!|MailAdd //分别储存了名字、电话及邮箱
读取时方法如下:
----------------------------------------------------
$data=explode('|!:!|'dbmfetch($dbmid,$key));
//则$data[o]对应名字,$data[1]对应电话,$data[2]对应邮箱
----------------------------------------------------
2、DBM本身储存数据没有任何物理顺序,只能通过自己的处理(见下文)来排序!
3、DBM不像文本,把一个db从这个主机转移到另一个主机上是会出错的,即一旦建立一个db文件,就不能转移!
4、DBM在NT下面一个key对应的值的长度不能超过1k个字符,故在NT下不能使用DBM保留一些有长度问题的东西!
5、关于DBM中使用中文的KEY:DBM的key不能使用中文,笔者在一共程序中试过,如果使用中文作为key的话当key一多(大约20)就会出现无法遍历搜索的问题!
四、用DBM做无序数据库:
用DBM做无序数据库(即数据无顺序概念)十分简单,比文本数据库要简单的多!比如下面是一个让用户输入用户名后给出用户电话的程序:
----------------------------------------------------
if(isset($userid)){
$data=dbmopen("path","r");
if(dbmexists($data,$userid))echo $no=dbmfetch($data,$userid);
else echo "UserID Error!";
dbmclose($data);
}else{
?>
请输入您的用户名:
----------------------------------------------------
五、用DBM做有序数据库:
因为DBM没有对数据进行排序,所以对于一些按一定顺序输出的程序(如刚才的程序改为显示所有用户的电话)则比较棘手,需要人为排序,下面笔者给出两种方法:
1、排序数据法:在这种方法里我们专门用一个key所对应的值来记录顺序,那个key我们人为命名为sort,sort对应值如下:
data1's key|data2's key|data3's key|........|data n's key
其中data n's dey的长度需要一定(笔者使用的是时间方法,如下程序可以生成key:)
----------------------------------------------------
function getkey(){
$date=date("ymdHis");
return $date;
}
----------------------------------------------------
这样用getkey()可以得到一个类似001203114950的12位key,而每个key就对应它自己的值(在这里用户的电话);而用substr($sort,$i*13,12)就可以读出第i个用户的key,下面是显示列表的代码:
----------------------------------------------------
//首先要知道有多少个用户,可以专门开一个num来记录
$data=dbmopen("path","r"); //打开数据库
$sort=dbmfetch($data,"sort"); //读取sort
for($i=0;$i$key=substr($sort,$i*13,12); //安顺序取得key
$telno=dbmfetch($data,$key); //读出key对应的value
echo $i+1." User's TelNO is ".$telno."
";
}
dbmclose($data);
?>
----------------------------------------------------
2、数组排序法:在这里感谢无伤兄,是他让笔者想起用数组来排序的。数组排序的基本原理是将整个DBM数据库的每条key读入数组,然后根据每个key的大小使用usort()等函数排序然后输出。
因为是对key的大小排序,所以key的长度没有什么限制,只要保证后加入的key大于先加入的key就可以实现先显示后加入的人,这里用time()来作为key。
下面是列表代码:
----------------------------------------------------
$data=dbmopen("path","r"); //打开数据库
$i=1;
for($key=dbmfirstkey($data);$key;$key=dbmnextkey($data,$key)){
$sort[$i]=$key;
$i++;
} //遍历取得所有的key
usort($sort); //安大小排序
for($i=0;$i
";
dbmclose($data);
----------------------------------------------------
以上两种方法在运行时占用内存(用于排序)第一种比第二种要大一点,不过对于运算量来讲,第一种方法则要小于第二种,至于想用哪种方法就随你意了(如果你兴趣,可以试试两者的CPU占用率)。
笔者在经过了N次(N>50)实验后完成了这篇文章仅以此献给穷苦的仍在用txt的朋友们相信也不是所有看CN的人都有自己的主机!如果有什么不明白的,来这里看看:www.zphp.com。
笔者不是什么高手,不过因为几乎找不到在PHP上使用DBM数据库的资料(包括英文资料,不过Perl方面的却不少)。最好留一个笔者用DBM写的论坛,大家可以试试速度:www.zphp.com/demo/dbmbbs/ !

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),