PHP之没有MySQL支持时的替代方案
一般个人免费主页空间都不会提供mysql支持,就是提供也很苛刻,所以寻找也个良好的替代方案很重要哦!
PHP的文件处理功能很强大,所以可以用文件的存取来代替来!
(要知道没有数据库的时候,什么都是用文件组织的哦!呵呵!),其中个数据项用非凡符号分割,我采用的是“||”,方便通过explode()函数读取单个记录!
其实这里数据库的思想还是可以用到的!象数据库的索引!
所以必须先做个索引文件!(这样说也并不正确)
就以留言本来说吧:
主要文件是:
index.database
其结构如下:
留言人姓名||留言人性别||留言时间||留言内容存放位置||feiyn(这项是方便读取时的被‘n’干恼的!
每条存储一行可以方便的通过PHP的fgets()函数读取,或者file()函数读取每行到数组
为了防止多人同是对数据的写入冲突,故还需要加锁(也用文件实现)
以下是写入代码
//必须传入以下参量:
//留言人姓名 $name
//留言人性别 $sex
//留言时间 $time
//留言内容存放位置 $savePosite
$indexFile="index.database";
$indexFileLock=$indexFile."Lock";
$message=$name."||".$sex."||".$time."||".$savePosite."||feiy||";//这就是要写入的记录
while(file_exists($indexFileLock)) $temp ; //检测是否已加锁
fclose(fopen($indexFileLock,"w")); //如没有则进入并加锁避免同是访问冲突
$fp=fopen($indexFile,"a");
fputs($message,strlen($message));
fclose($fp);
unlink($indexFileLock);//解锁
?>
读取代码
$indexFile="index.database";
$indexFileLock=$indexFile."Lock";
while(file_exists($indexFileLock)) $temp ; //检测是否已加锁
fclose(fopen($indexFileLock,"w")); //如没有则进入并加锁避免同是访问冲突
$ary=file($indexFile);
unlink($indexfileLock);//解锁
for($i=0;$i
echo("name:".$tempAry[0]);
echo("sex:".$tempAry[1]);
echo("sex:".$tempAry[2]);
echo("savePosite:",$tempAry[3]);//可以从该地址读取留言内容
}
?>

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

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

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

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

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

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 is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

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

The following resources contain additional information on CakePHP. Please use them to get more in-depth knowledge on this.
