Introduction to PHP and PostgreSQL - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to PHP and PostgreSQL

Description:

Introduction to PHP and MySQL Author: Lisa Dipippo Last modified by: Lisa DiPippo Created Date: 10/21/2003 8:11:01 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 19
Provided by: LisaDi9
Category:

less

Transcript and Presenter's Notes

Title: Introduction to PHP and PostgreSQL


1
Introduction to PHP and PostgreSQL
  • CSC 436 Fall 2008
  • Slides based on PHP manual Getting Started from
    http//www.php.net/tut.php

2
PHP - Overview
  • Introduction
  • Tutorial
  • Installation
  • PostgreSQL with PHP
  • Extended example

3
Introduction
  • PHP (recursive acronym for "PHP Hypertext
    Preprocessor")
  • widely-used Open Source general-purpose scripting
    language
  • especially suited for Web development
  • can be embedded into HTML.
  • lthtmlgt
  • ltheadgt
  • lttitlegtExamplelt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php echo "Hi, I'm a PHP script!" ?gt
  • lt/bodygt
  • lt/htmlgt
  • See this example here

4
What can PHP do?
  • Server-side scripting
  • most traditional and main target field
  • need three things to make this work
  • PHP parser (CGI or server module)
  • webserver with connected PHP installation
  • web browser
  • Command line scripting
  • PHP script to run it without any server or
    browser
  • need the PHP parser to use it this way
  • Writing client-side GUI applications
  • not the best language to write windowing
    applications
  • use PHP-GTK to write such programs

5
Tutorial
  • What do I need?
  • Your first PHP-enabled page
  • Something Useful
  • Dealing with Forms
  • What's next?

6
What do I need?
  • Assume that your server has support for PHP
    activated and that all files ending in .php are
    handled by PHP
  • Create your .php files and put them in your web
    directory and the server will magically parse
    them for you
  • No need to compile anything
  • Develop locally
  • install a web server, such as Apache
  • install PHP
  • install a database as well, such as PostgreSQL.

7
First PHP-enabled page
  • Create a file named hello.php
  • Put it in your web servers directory
  • lthtmlgt
  • ltheadgt
  • lttitlegtPHP Testlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php echo "ltpgtHello Worldlt/pgt" ?gt
  • lt/bodygt
  • lt/htmlgt
  • Use your browser to access the file

8
Something useful
  • check what sort of browser the person viewing the
    page is using
  • check the user agent string that the browser
    sends as part of its HTTP request
  • stored in a variable
  • always start with a dollar-sign in PHP
  • _SERVER"HTTP_USER_AGENT".
  • _SERVER is a special reserved PHP variable that
    contains all web server information
  • lthtmlgt
  • ltheadgt
  • lttitlegtPHP Testlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php echo _SERVER"HTTP_USER_AGENT" ?gt
  • lt/bodygt
  • lt/htmlgt (see this file)

9
Variables
  • Variables represented by a dollar sign followed
    by the name of the variable
  • name is case-sensitive.
  • name starts with a letter or underscore, followed
    by any number of letters, numbers, or underscores
  • type is assigned by value
  • lt?php
  • var "Bob"
  • Var "Joe"
  • echo "var, Var" // outputs "Bob, Joe"
  • x 1
  • x abc // type can change if value changes
  • 4site 'not yet' // invalid starts with a
    number _4site 'not yet' // valid starts with
    an underscore
  • ?gt

10
Arrays
  • created by the array() language-construct
  • takes a certain number of comma-separated key gt
    value pairs
  • lt?php
  • arr array("foo" gt "bar", 12 gt true)
  • echo arr"foo" // bar
  • echo arr12 // 1
  • ?gt (see it here)

11
Arrays (cont.)
  • lt?php // Create a simple array.
  • array array(1, 2, 3, 4, 5)
  • print_r(array)
  • // Now delete every item, but leave the array
  • // itself intact
  • foreach (array as i gt value)
    unset(arrayi)
  • print_r(array)
  • // Append an item (note that the new key is 5,
  • // instead of 0 as you might expect).
  • array 6
  • print_r(array)
  • // Re-index
  • array array_values(array)
  • array 7
  • print_r(array)
  • ?gt (see result here)

12
Dealing with forms
  • HTML Forms (GET and POST)
  • form is submitted to a PHP script
  • information from that form is automatically made
    available to the script
  • forms.php
  • ltform action"foo.php" method"POST"gt
  • Name ltinput type"text" name"username"gtltbrgt
    Email ltinput type"text" name"email"gtltbrgt
    ltinput type"submit" name"submit" value"Submit
    me!"gt
  • lt/formgt

13
Forms foo.php
  • lt?php // Available since PHP 4.1.0
  • print _POST'username'
  • print _REQUEST'username'
  • import_request_variables('p', 'p_')
  • print p_username
  • // Available since PHP 3. As of PHP 5.0.0, these
    long
  • // predefined variables can be disabled with the
  • // register_long_arrays directive.
  • print HTTP_POST_VARS'username'
  • // Available if the PHP directive
    register_globals on.
  • // As of PHP 4.2.0 the default value of
  • // register_globals off.
  • // Using/relying on this method is not preferred.
  • print username ?gt (see result here)

14
Another forms example
  • info_form.php
  • ltform actionshow_answers.php method"POST"gt
  • Your name ltinput type"text" name"name" /gt
  • Your age ltinput type"text" name"age" /gt
  • ltinput type"submit"gt
  • lt/formgt
  • show_answers.php
  • Hi
  • lt?php echo _POST"name" ?gt.
  • You are lt?php echo _POST"age" ?gt years old.
  • (see results here)

15
Installation
  • PHP is installed on the Linux servers in the CSC
    department
  • You can install it on your own machine if you
    want
  • Refer to the PHP manual for instructions on
    installation
  • http//www.php.net/manual/en/install.php

16
PosgreSQL with PHP
  • Assume the following database table and data
  • CREATE TABLE employees
  • ( id tinyint(4) DEFAULT '0' NOT NULL
    AUTO_INCREMENT,
  • first varchar(20),
  • last varchar(20),
  • address varchar(255),
  • position varchar(50),
  • PRIMARY KEY (id),
  • UNIQUE id (id))
  • INSERT INTO employees VALUES
  • (1,'Bob','Smith','128 Here St, Cityname',
  • 'Marketing Manager')
  • INSERT INTO employees VALUES
  • (2,'John','Roberts','45 There St ,Townville',
  • 'Telephonist')
  • INSERT INTO employees VALUES
  • (3,'Brad','Johnson','1/34 Nowhere Blvd,
    Snowston',
  • 'Doorman')

17
PostgreSQL Example (cont)
  • lthtmlgt
  • ltbodygt
  • lt?php
  • db pg_connect(dbnamesam usersam
    passwordiam")
  • or die(Couldnt Connect .pg_last_error(db))
  • querySELECT FROM employees
  • result pg_query(db,query)
  • or
  • die(Error in query query..pg_last_error(db)
    )
  • rows pg_num_rows(result)
  • if (rows gt 0)
  • for (i0 iltrows i)
  • my_row pg_fetch_array(result, i,
    PGSQL_ASSOC)
  • printf("First Name
    s",my_rowfirst)
  • printf("Last Name s", my_rowlast)
  • printf("Address s", my_rowaddress)
  • printf("Position s",my_row"position)
  • printf(\n)

18
Extended Example Toy Catalog
  • Toy Catalog as described in class
  • Assume database tables created
  • Assume data inserted into data
  • Start with html form page
Write a Comment
User Comments (0)
About PowerShow.com