Speed up development with full-stack environments for every branch.

Learn More

How to Create a file-based database and Insert data into it using DBA functions [PHP]

190 Runs 1389 Views 123 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP DBA module
 * Create a database and inserting data
 */

//setting div style for nicer display
$divStyle = ' background-color:#E8E8E3;
            padding:10px;
            color:#000;
            font-size:16px;
            width:500px;
            margin:0 auto;';

//create a database with create  mode
$id = dba_open("test.db", "n", "cdb");


//inserting some data into the databse
dba_insert('father/name', 'John', $id);
dba_insert('mother/name', 'Gina', $id);
dba_insert('son/name', 'Duncan', $id);
dba_insert('daughter/name', 'Marry', $id);
dba_insert('grandmother/name', 'Daria', $id);
dba_insert('grandfather/name', 'Lile', $id);

//close the database connection 
//and allowing the system to save data
dba_close($id);

//check to see if the data was written to the database

//open the database in read mode
$id = dba_open("test.db", "r", "cdb");

//accessing the first key
$key = dba_firstkey($id);
//looping through all the keys from the database
//and display every key and it's value
echo '<div style="' . $divStyle . '">
<b><u>These are the elements  that we previous wrote to the "test" database:</u></b>
<br /><br />';
while ($key != NULL) {
    echo $key . ': ' . dba_fetch($key, $id) . '<br />';
    $key = dba_nextkey($id);
}
echo '</div>';

//close the database connection
dba_close($id);
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).