Did you ever need to use txt files as Database with Php?
Proposal
In some small projects, I needed a simple database without any server need. Mysql, PostgreSql, MongoDb, Cassandra etc. All needs a server and drivers for programming languages. And there is no need to use separate applications for small (fast development) projects.
Then I thought to write a small library to use Txt files as database.
Logic is simple;
Your directory = Your database directory,
Your files = Your tables,
File contents = Table rows
Installation
Go to repository page on github and download the latest version.
then require the library in your php file.
require("txtdb.class.php");
$db = new TxtDb();
Mockups / Examples
Here are some examples how to use library.
- Insert
This method gets two parameters.
First is "table name", second is our data in array.
$db->insert("teachers", [
"name" => "John Doe",
"email" => "[email protected]"
]);
- Update
This method gets three parameters.
First is "table name", second is our data in array and third is "where situation"
$db->update("teachers", [
"name" => "John Doe",
"email" => "[email protected]"
],1);
- Delete
$db->delete("students",1);
Important: If you dont set second param, this method deletes all contents in file!
- Select
If you want to return all data as array
$students = $db->select('students');
foreach($students as $student){
print $student->name;
}
If you want to get only one row by ID
$user = $db->select('students',1);
echo $user->name;
//output
John Doe
If you need where situation, you can set the second parameter as array
$teachers =$db->select('teachers',array('name' =>'John Doe"'));
foreach ($teachers as $teacher) {
echo 'Name : '.$teacher->Name.';
echo 'Surname : '.$teacher->Surname.';
echo 'Age : '.$teacher->Age.';
echo 'Email : '.$teacher->Email.';
}
Benefits
This library solves slow development problem. Due t it doesn't require any driver, its efficient for fast development. Its only for small projects or small components of your projects. Be aware that don't use this in your whole (big) project.
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
It cannot be considered as a
Development
contribution. As the reposiyory is more than 1 year old.You can contact us on Discord.
[utopian-moderator]