php & gdbm tutorial - v3 - dalhousie...

23
PHP & GDBM Tutorial By Naureen Nizam May 17, 2011

Upload: doanh

Post on 07-Feb-2019

226 views

Category:

Documents


0 download

TRANSCRIPT

PHP & GDBM Tutorial

By Naureen Nizam

May 17, 2011

Agenda

Introduction on PHP

PHP 101 – Examples

Introduction on GDBM

Using PHP & GDBM

Example

Resources

Questions

Introduction to PHP

PHP: Hypertext Preprocessor

Created by Rasmus Lerdorf in 1995

Scripting language - server-side webdevelopment Dynamic Web Pages

Command line scripting

Client-side GUI applications

Open Source

Comparable to ASP

What can it do?

Dynamic generation of web-page content

Database interaction

Processing of user supplied data

Email

File handling

Text processing

Network interaction

PHP 101

PHP is embedded within HTML pageswith tags: <?php … ?>

PHP 101 (cont’d)

Each line must be terminated by asemi-colon.

Literals: All strings must be enclosed in single of

double quotes: ‘Hello’ or “Hello”.

Numbers are not in enclosed in quotes: 1or 45 or 34.564

Booleans (true/flase) can be writtendirectly as true or false.

PHP 101 (cont’d)

Comments:

// This is a comment

# This is also a comment

/* This is a commentthat is spread overmultiple lines */

PHP 101 (cont’d)

Displaying Data

Two language constructs: print() and echo()

Can be used with or without brackets.

<?php

echo ‘Hello World!<br />’;echo(‘Hello World!<br />’);

print ‘Hello World!<br />’;print(‘Hello World!<br />’);

?>

PHP 101 (cont’d)

Escaping Characters

Some characters are considered ‘special’

Escape these with a backslash \

Example: echo ‘Claire O\’Reilly ’;

PHP 101 (cont’d)

Variables

Need a place to store a value (string,number, etc.)

Naming: $ followed by variable name

(must start with a letter or an underscore)

Case sensitive $variable differs from $Variable

PHP 101 (cont’d)

<?php$name = ‘John’;$age = 28;echo $name;echo ’ is ‘;echo $age;// John is 28?>

Example

PHP 101 (cont’d)

Constants (unchangeable variables) are definedusing the define() function. By convention, constant names are usually in

UPPERCASE.<?phpdefine(‘NAME’,‘John’);define(‘AGE’,28);echo NAME;echo ’ is ‘;echo AGE;// John is 28?>

PHP 101 (cont’d)

Statements IF statement

if (<condition>) {

//php code goes here

}

else {

//php code goes here

}

For loop

for($i=0;$i < 10;$++i) {echo(“the value is :”. $i);

}

PHP 101 (cont’d)

Functions

function my_func(<parameters>) {

//do something in the function

}

PHP 101 (cont’d)

Arrays each element has a value, data stored in the

element.

And has a key by which the element can bereferred to. $staff[] = “Jack”;

$staff[] = “John”;

OR

$staff = array (“Jack”, “John”);

Print: print_r($staff);

PHP 101 (cont’d)

Useful functions: sort(); //sort an array (lowest to highest) count(); //counts all the elements in an array split(); // split string into an array by regular expression array_push(); //push one or more elements to the end of the array trim(); //strip whitespaces from beginning and end of a string unset(); //unset a given variable isset(); //determine if a variable is set and is not NULL explode(); //split a string by string into an array implode(); //join array elements with a string preg_match(); //perform a regular expression match preg_replace(); //perform a regular expression match $fh = fopen($File, 'r') or exit("Unable to open file!");

Introduction to GDBM

GNU Database Manager Database routines Use extensive hashing Implementation of a key->value database The five basic functions of gdbm:

gdbm_open (open a database) gdbm_store (store a key,value pair in a database) gdbm_fetch (given a key get the associated value from

the database) gdbm_delete (delete a key,value pair from the

database) gdbm_close (close a database)

Using PHP and GDBM

PHP must be compiled with GDBMenabled (DBA module) on the server ./configure …' '--with-gdbm' '—

DBA support => enabled

Supported handlers => gdbm

bluenose.cs.dal.ca (Done)

hector.cs.dal.ca (Pending - this week)

Verify using <?php phpinfo(); ?>

Steps

1. Open databaseCreate: $dbh= dba_open('cars.db', 'c', 'gdbm')Read: $dbh= dba_open(‘cars.db','r','gdbm');

2. Insert into the databasedba_insert($key, $value, $dbh);

3. Retrieve values from the databasedba_fetch($key, $dbh); //to fetch a specific keydba_firstkey($dbh); //to fetch the first keydba_nextkey($dbh); //to fetch the next keydba_delete("key", $dbh); //to delete a specific keydba_exists("key", $id); //to see if the key exist

4. Close Databasedba_close($dbh);

Example: PHP & GDBM

<?php

$dbh= dba_open('cars.db', 'c', 'gdbm') or die($php_errormsg);

//insert data$index=0;$value="Honda";dba_insert($index, $value, $dbh);

$index=1;$value="Toyota";dba_insert($index, $value, $dbh);

for ($key = dba_firstkey($dbh); $key !== false; $key = dba_nextkey($dbh)){

$value = dba_fetch($key, $dbh);print "$key: $value\n";

}

dba_close($dbh);?>

Create file [filename].php

Example (cont’d)

Make sure file is executable

Run file from command line

php [filename].php

A binary file called cars.db should becreated

Resources

Questions

Naureen Nizam

[email protected]