Adding Complex Data Structures to a Database

All data in a DBM-like database is extracted in string format, so you are limited to storing integers, strings, and doubles. Any other data type will be lost. Let’s try to store an array, for example:

$array = array( 1, 2, 3, 4 );
$dbh = dba_open( "./data/test", "c", "gdbm" ) or die("Couldn't open test");
dba_insert("arraytest", $array, $dbh );
print gettype( dba_fetch("arraytest", $dbh) );
// prints "string"

We create an array and store it in the variable $array. We then open a database and attempt to insert an element called "arraytest", passing it the $array variable as the value. We then test the return type from dba_fetch() when attempting to access "arraytest" and ascertain that a string has ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.