Local Database

September 21, 2008

A friend learning PHP, recently asked me how they can mimic a database when the site they are working on does not have access to one. The following will show you how we can use an Array to create a local database and nested Arrays to create tables, rows and columns as key/value pairs.
 

The Source

The data for today comes from MySQL 5.0 Manual, 3.3.3. Loading Data into a Table.

name owner species sex birth death
Fluffy Harold cat f 1993-02-04
Claws Gwen cat m 1994-03-17
Buffy Harold dog f 1989-05-13
Fang Benny dog m 1990-08-27
Bowser Diane dog m 1979-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11
Whistler Gwen bird 1997-12-09
Slim Benny snake m 1996-04-29

 

The Skeleton

We begin by creating an array that represents our database, $db = array (). We follow that by defining our table, "pets" => array () as key/value pairs. Our tables' name is set as the key and an empty array is set as its value.

  1. <?php
  2.  
  3. $db = array (
  4.     "pets" => array (
  5.    
  6.     )
  7. );
  8.  
  9. ?>

 

Adding The First Row

Using the data from our source, lets add our first row. Each row will be represented by an array with key/value pairs; the column name of our table is set as the key and the rows value for that column is set as the value..

  1. <?php
  2.  
  3. $db = array (
  4.     "pet" => array (
  5.         array (
  6.             "name" => "Fluffy",
  7.             "owner" => "Harold",
  8.             "species" => "cat",
  9.             "sex" => "f",
  10.             "birth" => "1993-02-04",
  11.             "death" => ""
  12.         )
  13.     )
  14. );
  15.  
  16. ?>

 
Continue adding the rest of the data from our source. To differentiate each row, ensure you place a comma between each array but not on the last array. Once you are done adding each row, save your file as db.php. Here a quick look at three completed rows.

  1. <?php
  2.  
  3. $db = array (
  4.     "pet" => array (
  5.         array (
  6.             "name" => "Fluffy",
  7.             "owner" => "Harold",
  8.             "species" => "cat",
  9.             "sex" => "f",
  10.             "birth" => "1993-02-04",
  11.             "death" => ""
  12.         ),
  13.         array (
  14.             "name" => "Claws",
  15.             "owner" => "Gwen",
  16.             "species" => "cat",
  17.             "sex" => "m",
  18.             "birth" => "1994-03-17",
  19.             "death" => ""
  20.         ),
  21.         array (
  22.             "name" => "Buffy",
  23.             "owner" => "Harold",
  24.             "species" => "dog",
  25.             "sex" => "f",
  26.             "birth" => "1989-05-13",
  27.             "death" => ""
  28.         )
  29.     )
  30. );
  31.  
  32. ?>

 

Importing The Data

Now that db.php is ready, lets continue by creating a new file, showPets.php; it will be this file that we will import our database and show its results. Lets begin by importing our database onto our new file, include_once("db.php") and to ensure our array is looking like a database, lets print the results using print_r().

  1. <?php
  2.  
  3. include_once("db.php");
  4. print_r($db);
  5.  
  6. ?>

 

View The Results

Upload or if you have a local web server running, open showPets.php with your preferred web browser; i hope your preferred browser is not IE. If you're $db array has no syntax errors, you will see...

  1. Array ( [pet] => Array ( [0] => Array ( [name] => Fluffy [owner] => Harold [species] => cat [sex]
  2. => f [birth] => 1993-02-04 [death] => ) [1] => Array ( [name] => Claws [owner] => Gwen
  3. [species] => cat [sex] => m [birth] => 1994-03-17 [death] => ) [2] => Array ( [name] => Buffy
  4.  [owner] => Harold [species] => dog [sex] => f [birth] => 1989-05-13 [death] => ) [3] => Array
  5. ( [name] => Fang [owner] => Benny [species] => dog [sex] => m [birth] => 1990-08-27 [death]
  6. => ) [4] => Array ( [name] => Bowser [owner] => Diane [species] => dog [sex] => m [birth] =>
  7.  1979-08-31 [death] => 1995-07-29 ) [5] => Array ( [name] => Chirpy [owner] => Gwen [species]
  8.  => bird [sex] => f [birth] => 1998-09-11 [death] => ) [6] => Array ( [name] => Whistler [owner]
  9.  => Gwen [species] => bird [sex] => [birth] => 1997-12-09 [death] => ) [7] => Array ( [name] =>
  10.  Slim [owner] => Benny [species] => snake [sex] => m [birth] => 1996-04-29 [death] => ) ) )

 
To view a structured version of the results, go to View Source.

  1. Array
  2. (
  3.     [pet] => Array
  4.         (
  5.             [0] => Array
  6.                 (
  7.                     [name] => Fluffy
  8.                     [owner] => Harold
  9.                     [species] => cat
  10.                     [sex] => f
  11.                     [birth] => 1993-02-04
  12.                     [death] =>
  13.                 )
  14.  
  15.             [1] => Array
  16.                 (
  17.                     [name] => Claws
  18.                     [owner] => Gwen
  19.                     [species] => cat
  20.                     [sex] => m
  21.                     [birth] => 1994-03-17
  22.                     [death] =>
  23.                 )
  24.  
  25.             [2] => Array
  26.                 (
  27.                     [name] => Buffy
  28.                     [owner] => Harold
  29.                     [species] => dog
  30.                     [sex] => f
  31.                     [birth] => 1989-05-13
  32.                     [death] =>
  33.                 )
  34.  
  35.             [3] => Array
  36.                 (
  37.                     [name] => Fang
  38.                     [owner] => Benny
  39.                     [species] => dog
  40.                     [sex] => m
  41.                     [birth] => 1990-08-27
  42.                     [death] =>
  43.                 )
  44.  
  45.             [4] => Array
  46.                 (
  47.                     [name] => Bowser
  48.                     [owner] => Diane
  49.                     [species] => dog
  50.                     [sex] => m
  51.                     [birth] => 1979-08-31
  52.                     [death] => 1995-07-29
  53.                 )
  54.  
  55.             [5] => Array
  56.                 (
  57.                     [name] => Chirpy
  58.                     [owner] => Gwen
  59.                     [species] => bird
  60.                     [sex] => f
  61.                     [birth] => 1998-09-11
  62.                     [death] =>
  63.                 )
  64.  
  65.             [6] => Array
  66.                 (
  67.                     [name] => Whistler
  68.                     [owner] => Gwen
  69.                     [species] => bird
  70.                     [sex] =>
  71.                     [birth] => 1997-12-09
  72.                     [death] =>
  73.                 )
  74.  
  75.             [7] => Array
  76.                 (
  77.                     [name] => Slim
  78.                     [owner] => Benny
  79.                     [species] => snake
  80.                     [sex] => m
  81.                     [birth] => 1996-04-29
  82.                     [death] =>
  83.                 )
  84.  
  85.         )
  86.  
  87. )

 

Tags: , , ,

Leave a Reply

You must be logged in to post a comment.