bencagri avatar

Did you ever need to use txt files as Database with Php?

bencagri

Published: 23 Jan 2018 › Updated: 23 Jan 2018Did you ever need to use txt files as Database with Php?

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" => "john@google.com"
]);

- 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" => "john@google.com"
],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

Leave Did you ever need to use txt files as Database with Php? to:

Written by

Back-end developer

Read more #utopian-io posts


Best Posts From bencagri

We have not curated any of bencagri's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From bencagri