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 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 |
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.
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..
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.
-
<?php
-
-
array (
-
"name" => "Fluffy",
-
"owner" => "Harold",
-
"species" => "cat",
-
"sex" => "f",
-
"birth" => "1993-02-04",
-
"death" => ""
-
),
-
array (
-
"name" => "Claws",
-
"owner" => "Gwen",
-
"species" => "cat",
-
"sex" => "m",
-
"birth" => "1994-03-17",
-
"death" => ""
-
),
-
array (
-
"name" => "Buffy",
-
"owner" => "Harold",
-
"species" => "dog",
-
"sex" => "f",
-
"birth" => "1989-05-13",
-
"death" => ""
-
)
-
)
-
);
-
-
?>
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().
-
<?php
-
-
include_once("db.php");
-
-
?>
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...
-
Array ( [pet] => Array ( [0] => Array ( [name] => Fluffy [owner] => Harold [species] => cat [sex]
-
=> f [birth] => 1993-02-04 [death] => ) [1] => Array ( [name] => Claws [owner] => Gwen
-
[species] => cat [sex] => m [birth] => 1994-03-17 [death] => ) [2] => Array ( [name] => Buffy
-
[owner] => Harold [species] => dog [sex] => f [birth] => 1989-05-13 [death] => ) [3] => Array
-
( [name] => Fang [owner] => Benny [species] => dog [sex] => m [birth] => 1990-08-27 [death]
-
=> ) [4] => Array ( [name] => Bowser [owner] => Diane [species] => dog [sex] => m [birth] =>
-
1979-08-31 [death] => 1995-07-29 ) [5] => Array ( [name] => Chirpy [owner] => Gwen [species]
-
=> bird [sex] => f [birth] => 1998-09-11 [death] => ) [6] => Array ( [name] => Whistler [owner]
-
=> Gwen [species] => bird [sex] => [birth] => 1997-12-09 [death] => ) [7] => Array ( [name] =>
-
Slim [owner] => Benny [species] => snake [sex] => m [birth] => 1996-04-29 [death] => ) ) )
To view a structured version of the results, go to View Source.
-
Array
-
(
-
[pet] => Array
-
(
-
[0] => Array
-
(
-
[name] => Fluffy
-
[owner] => Harold
-
[species] => cat
-
[sex] => f
-
[birth] => 1993-02-04
-
[death] =>
-
)
-
-
[1] => Array
-
(
-
[name] => Claws
-
[owner] => Gwen
-
[species] => cat
-
[sex] => m
-
[birth] => 1994-03-17
-
[death] =>
-
)
-
-
[2] => Array
-
(
-
[name] => Buffy
-
[owner] => Harold
-
[species] => dog
-
[sex] => f
-
[birth] => 1989-05-13
-
[death] =>
-
)
-
-
[3] => Array
-
(
-
[name] => Fang
-
[owner] => Benny
-
[species] => dog
-
[sex] => m
-
[birth] => 1990-08-27
-
[death] =>
-
)
-
-
[4] => Array
-
(
-
[name] => Bowser
-
[owner] => Diane
-
[species] => dog
-
[sex] => m
-
[birth] => 1979-08-31
-
[death] => 1995-07-29
-
)
-
-
[5] => Array
-
(
-
[name] => Chirpy
-
[owner] => Gwen
-
[species] => bird
-
[sex] => f
-
[birth] => 1998-09-11
-
[death] =>
-
)
-
-
[6] => Array
-
(
-
[name] => Whistler
-
[owner] => Gwen
-
[species] => bird
-
[sex] =>
-
[birth] => 1997-12-09
-
[death] =>
-
)
-
-
[7] => Array
-
(
-
[name] => Slim
-
[owner] => Benny
-
[species] => snake
-
[sex] => m
-
[birth] => 1996-04-29
-
[death] =>
-
)
-
-
)
-
-
)



