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

Learn More

How to Read data from a file-based database using DBA functions [PHP]

41 Runs 333 Views 168 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php
/*
 * PHP DBA module
 * Open and Read data from a database
 */
 
//setting div style for nicer display
$divStyle = ' background-color:#E8E8E3;
            padding:10px;
            color:#000;
            font-size:16px;
            width:400px;
            margin:0 auto;';
            
//open a database with read access mode
//the database test.db is already created
$id = dba_open("test.db", "r", "cdb");

//check if the database was open or not
if (!$id) {
        echo "dba_open failed\n";
        exit;
}

//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 of 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).