Home >Backend Development >PHP Tutorial >PHP alternatives when there is no MySQL support_PHP tutorial
Generally, free personal homepage spaces will not provide mysql support, and even if it is provided, it is very demanding, so it is important to find a good alternative!
PHP's file processing function is very powerful, so you can use file access instead!
(You must know that when there is no database, everything is organized by files! Haha!). Each data item is separated by special symbols. I use "||" to facilitate reading a single record through the explode() function!
In fact, the idea of database can still be used here! Like a database index!
So you must make an index file first! (This is not correct)
Let’s take the guestbook as an example:
The main file is:
index.database
Its structure is as follows:
Name of the person who left the message |
Each line stored can be easily read through PHP's fgets() function, or the file() function reads each line into an array
In order to prevent multiple people from writing data at the same time, locking is also required (also implemented using files)
The following is the written code
//The following parameters must be passed in:
//Name of the messager $name
//The gender of the messager $sex
//Message time $time
//Message content storage location $savePosite
$indexFile="index.database";
$indexFileLock=$indexFile."Lock";
$message=$name."||".$sex."||".$time."||".$savePosite."||feiy||";//This is the record to be written
while(file_exists($indexFileLock)) $temp; //Check whether it is locked
fclose(fopen($indexFileLock,"w")); //If not, enter and lock to avoid the same access conflict
$fp=fopen($indexFile,"a");
fputs($message,strlen($message));
fclose($fp);
unlink($indexFileLock);//Unlock
?>
Read code
$indexFile="index.database";
$indexFileLock=$indexFile."Lock";
while(file_exists($indexFileLock)) $temp; //Check whether it is locked
fclose(fopen($indexFileLock,"w")); //If not, enter and lock to avoid the same access conflict
$ary=file($indexFile);
unlink($indexfileLock);//Unlock
for($i=0;$i
echo("name:".$tempAry[0]);
echo("sex:".$tempAry[1]);
echo("sex:".$tempAry[2]);
echo("savePosite:",$tempAry[3]);//The message content can be read from this address
}
?>