by Andres Baravalle
You should now be familiar with Core PHP, including:
strpos(
))A database is a organised collection of data. Database Management Systems are the software that manage and control the access to the database (Connolly and Begg, 2010).
A DBMS "works with the operating system to store and modify data and to make data accessible in a variety of meaningful and authorised ways" (Brown et al., 2011).
The aim of DBMSes is to allow users and programmers to select data from storage by referring to the content of the data, not by their physical location.
Before DBMSes, programmers had to develop their own functions to create, read, update and delete (CRUD) data.
That had implications in issues as maintainability, speed of development and performance.
There are (many) different types of databases but in L/WAMP applications the most common architectures are using relational databases, NoSQL databases or flat-file databases.
All databases implement a set of CRUD functions: to create, read, update and delete records.
The relational database is the most common and widespread architecture for organising structured data. Content is stored in tables and accessed through a specialist language, SQL. In many cases, a graphical user interface is available to simplify the interaction with the databases.
Relational DBMSes commonly used in web applications include Oracle (proprietary), SQL Server (proprietary), MySQL/MariaDB (Open Source), PostgreSQL (Open Source) and SQLite (public domain). They are all supported by PHP.
Apart from implementing CRUD functions (using SQL), transactions on relational databases are ACID:
NoSQL databases store the data in a loosely structured format and don't (typically) provide the same level of control (at database level) on the results returned (e.g. ordering). They are often highly optimized for simple retrieval and appending operations, with the goal of higher performance.
NoSQL DBMSes commonly used in web applications include:
CouchDB | Cassandra | MongoDB |
They are all supported by PHP.
Flat file databases store the data into flat text files (e.g. txt files). You will normally need to develop the CRUD functions to interact with your data storage and while ensuring the security of the data stored.
Flat file databases are typically used only when other solutions are not available or in circumstances where the complexity of the application to develop is minimal.
<?php
// simple file with user ids, surnames and names
// e.g.:
// u111111#John#Brown
// u111112#John#Johnson
// etc.
$file = file("students.txt");
// lets find all the users who have John as name
$needle = "JOHN";
$results = array();
while (list($key, $val) = each($file)) {
// $val is a string - we need to split it to get the name
$student = explode("#", $val);
if(strpos($student[1], $needle)!==FALSE) {
$results[] = $student;
}
}
print_r($results);
?>
This week's activities are based on a common scenario where we are using (some) CRUD functions to interact with a database of students and their marks in one or more exams.
For Activity #1, develop a flat text database including student number
, module name
and mark
.
After writing the database, create the functions to read the records and output as follows:
Student number | Module name | Mark |
---|---|---|
u1111111 | IM2081 | 41 |
u1111112 | IM2081 | 42 |
u1111113 | IM2081 | 43 |
XML is "a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable" (Wikipedia, 2013). XML can be used to store structured data in text files and the PHP DOM functions allow to easily interact with the document tree.
Web applications typically support more than one database, typically through the use of abstraction modules or abstraction classes.
This allows to use the same code base with different databases - with no need for re-development.
For example, the Drupal project natively supports MySQL/MariaDB, PostgreSQL or SQLite, and supports SQL Server, Oracle and MongoDB with additional modules.
Widely used abstraction layers in PHP include:
Abstraction layers support code portability. When developing (correctly) using an abstraction layer, you can replace your DBMS.
E.g. you may code an application using MySQL as a back-end and then deploy it on a Oracle back-end.
Whichever abstraction layer you decide to use, this comes with a cost in terms of performance.
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
?>
Nothing, for most web applications.
What about very very very large web applications? For example web sites as:
We will not cover databases (much) more in depth.
For database support in PHP, please refer to the PHP on-line documentation.
MongoDB (from "huMONGOus" - perhaps alteration of huge + monstrous) is a NoSQL, Open Source database.
MongoDB was originally developed by 10gen, a company set up by Eliot Horowitz and Dwight Merriman (the founder of DoubleClick).
The aims of MongoDB where to:
MongoDB is mostly used for large scale web applications. We are going to use it largely to store objects - but it is much more powerful.
MongoDB is already installed on mastodon. To install MongoDB on your home computer, follow the instructions here and here.
Two steps are required: the first is installing the database server, the second to install the PHP module.
While almost no configuration is needed to install MongoDB, installing the PHP module requires being familiar with your php.ini
file settings. Do not start doing that lightly.
Be aware that MongoDB can easily grow into using several GBs - you may have to customise your configuration file if you want to trade performance for disk space (typically on development machines).
MongoDB can be used:
You are not going to be able to use MongoDB from the command line, but you will be exploring the other two options.
To connect to MongoDB from PHP, you need to create a Mongo object, passing username, password, server and port and database name.
The default host is localhost, and the default port is 27017 (you do not normally need to change these parameters)
Connecting to MongoDB database server (authenticating against a database named dbname
) at localhost port 27017:
<?php
$connection = new MongoClient("mongodb://username:password@localhost/database_name");
print_r($connection);
/*
Mongo Object
(
[connected] => 1
[status] =>
[server:protected] =>
[persistent:protected] =>
)
*/
?>
Once you have established the connection with the database server, you use it to access a database:
<?php
$db = $connection->dbname; // opens the database dbname
?>
If you have administrative rights for MongoDB and the database doesn't exist, MongoDB will automatically create the new database for you.
Once you have accessed a database, you can select a collection:
<?php
$collection = $db->addresses;
?>
Activities 2-4 will consist in creatinga collection with student numbers, module names and student marks (as in Activity #1).
As a first step, create a new collection called marks.
To do it, you will need to:
marks
(if not existing, it will be created automagically)MongoDB is used to store objects and documents:
<?php
// creating an array
$student1 = array(student_id => "u1111111", module_name => "im2801", mark = 41);
collection->insert($student1);
?>
<?php
$connection = new Mongo("mongodb://andres:password@localhost");
$collection = $connection->andres->students;
// creating an array
$student1 = array("student_id" => "u1111111", "module_name" => "im2801", "mark" => 41);
$collection->insert($student1);
$student1 = array("student_id" => "u1111112", "module_name" => "im2801", "mark" => 42);
$collection->insert($student2);
?>
Develop a form - like this:
Save the data into your MongoDB collection.
MongoDB uses unique identifiers for each document (called primary keys or objectids). Unless otherwise configured, MongoDB will automatically create an ObjectIds.
The ObjectId is composed of a timestamp, as well as information about the machine it was created on. As an object, it has methods that you can run.
The method findOne()
is used to find the first matching document in a collection.
<?php
// It can be called without parameters:
$document = $collection->findOne();
// or using an array with the paramters:
$document = $collection->findOne(array("module_name" => "im2801"));
print_r($document);
/*
Array (
[_id] => MongoId Object
(
[$id] => 528c2686eae33e20048b4567
)
[student_id] => u1111111
[module_name] => im2801
[mark] => 41
)
*/
?>
<?php
$cursor = $collection->find();
foreach ($cursor as $key => $val)
{
echo "$key: ";
print_r($val);
}
?>
Create a simple page that lists - in a table - all the data inserted in your collection.
Use the mockup below as guidance:
You can use the update method to update a collection. Update will accept three paramters:
MongoCollection::update(array $criteria, array $new_object [, array $options = array() ])
By default, MongoDB will update only the first matching document. For advanced options, refer to the documentation.
<?php
// this is replacing the entire document stored with the content of the array
$collection->update(array("student_id" => "u1111111", "module_name" => "im2801"), array("mark" => 45));
// this is actually just changing the value
$collection->update(array("student_id" => "u1111111", "module_name" => "im2801"), array("$set" => array("mark" => 45)));
?>
Removing data is as simple as adding data:
<?php
$criteria = array('_id'=> new MongoId('4ba667b0a90578631c9caea1'));
$collection->remove($criteria, array("justOne" => true));
?>
Connolly, T. M. and Begg, C. E. (2010) Database Systems: A Practical Approach to Design, Implementation and Management, 5/E, Pearson/Addison Wesley.
Brown, C., DeHayes, D., Hoffer, J., Martin, E. and Perkins, W. (2011) Managing information technology. 7th edn. New Jersey: Pearson Education. ISBN-10: 0132146320 or ISBN-13: 978-0132146326.
The topics covered in this lecture are not in your textbook. Follow the links included in the slides and below to improve your understanding of PHP and NoSQL and to be able to implement it correctly in your PHP applications.
php.net (2013). Mongo tutorial. Available from: http://us.php.net/manual/en/mongo.tutorial.php
Francia, S. (2012). MongoDB and PHP
Chodorow
(2013). MongoDB: The Definitive Guide
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License