1. Trang chủ
  2. » Công Nghệ Thông Tin

beginning php and postgresql 8

863 1.3K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • 1590595475

  • Contents

  • About the Authors

  • About the Technical Reviewers

  • Acknowledgments

  • Introduction

  • CHAPTER 1: An Introduction to PHP

    • History

      • PHP 4

      • PHP 5

    • General Language Features

      • Practicality

      • Power

      • Possibility

      • Price

    • Summary

  • CHAPTER 2: Installing and Configuring Apache and PHP

    • Installation

      • Obtaining the Distributions

      • The Installation Process

      • Testing Your Installation

      • Customizing the Unix Build

      • Customizing the Windows Build

      • Common Pitfalls

      • Viewing and Downloading the Documentation

    • Configuration

      • Managing PHP's Configuration Directives

      • PHP's Configuration Directives

    • Summary

  • CHAPTER 3: PHP Basics

    • Escaping to PHP

      • Default Syntax

      • Short-Tags

      • Script

      • ASP-Style

      • Embedding Multiple Code Blocks

    • Comments

      • Single-line C++ Syntax

      • Shell Syntax

      • Multiple-Line C Syntax

    • Output

      • print()

      • echo()

      • printf()

      • sprintf()

    • Datatypes

      • Scalar Datatypes

      • Compound Datatypes

      • Special Datatypes

      • Type Casting

      • Type Juggling

      • Type-Related Functions

      • Type Identifier Functions

    • Identifiers

    • Variables

      • Variable Declaration

      • Variable Scope

      • PHP's Superglobal Variables

      • Variable Variables

    • Constants

    • Expressions

      • Operands

      • Operators

    • String Interpolation

      • Double Quotes

      • Single Quotes

      • Heredoc

    • Control Structures

      • Execution Control Statements

      • Conditional Statements

      • Looping Statements

      • File Inclusion Statements

    • Summary

  • CHAPTER 4: Functions

    • Invoking a Function

    • Creating a Function

      • Passing Arguments by Value

      • Passing Arguments by Reference

      • Default Argument Values

      • Optional Arguments

      • Returning Values from a Function

      • Nesting Functions

      • Recursive Functions

      • Variable Functions

    • Function Libraries

    • Summary

  • CHAPTER 5: Arrays

    • What Is an Array?

    • Outputting Arrays

    • Creating an Array

    • Testing for an Array

    • Adding and Removing Array Elements

    • Locating Array Elements

    • Traversing Arrays

    • Determining Array Size and Uniqueness

    • Sorting Arrays

    • Merging, Slicing, Splicing, and Dissecting Arrays

    • Other Useful Array Functions

    • Summary

  • CHAPTER 6: Object-Oriented PHP

    • The Benefits of OOP

      • Encapsulation

      • Inheritance

      • Polymorphism

    • Key OOP Concepts

      • Classes

      • Objects

      • Fields

      • Properties

      • __set()

      • Constants

      • Methods

    • Type Hinting

    • Constructors and Destructors

      • Constructors

      • Destructors

    • Static Class Members

    • The instanceof Keyword

    • Helper Functions

    • Autoloading Objects

    • Summary

  • CHAPTER 7: Advanced OOP Features

    • Advanced OOP Features Not Supported by PHP

    • Object Cloning

      • Cloning Example

      • The __clone() Method

    • Inheritance

      • Class Inheritance

      • Inheritance and Constructors

    • Interfaces

      • Implementing a Single Interface

      • Implementing Multiple Interfaces

    • Abstract Classes

    • Reflection

      • Writing the ReflectionClass Class

      • Writing the ReflectionMethod Class

      • Writing the ReflectionParameter Class

      • Writing the ReflectionProperty Class

      • Other Reflection Applications

    • Summary

  • CHAPTER 8: Error and Exception Handling

    • Configuration Directives

    • Error Logging

    • Exception Handling

      • Why Exception Handling Is Handy

      • PHP's Exception-Handling Implementation

    • Summary

  • CHAPTER 9: Strings and Regular Expressions

    • Complex (Curly) Offset Syntax

    • Regular Expressions

      • Regular Expression Syntax (POSIX)

      • PHP's Regular Expression Functions (POSIX Extended)

      • Regular Expression Syntax (Perl Style)

    • Other String-Specific Functions

      • Determining the Length of a String

      • Comparing Two Strings

      • Manipulating String Case

      • Converting Strings to and from HTML

      • Alternatives for Regular Expression Functions

      • Padding and Stripping a String

      • Counting Characters and Words

    • Taking Advantage of PEAR: Validate_US

      • Installing Validate_US

      • Using Validate_US

    • Summary

  • CHAPTER 10: Working with the File and Operating System

    • Learning About Files and Directories

      • Parsing Directory Paths

      • File Types and Links

      • Calculating File, Directory, and Disk Sizes

      • Access and Modification Times

    • File Ownership and Permissions

    • File I/0

      • The Concept of a Resource

      • Newline

      • End-of-File

      • Opening and Closing a File

      • Reading from a File

      • Moving the File Pointer

      • Writing to a File

      • Reading Directory Contents

    • Executing Shell Commands

      • PHP's Built-in System Commands

    • System-Level Program Execution

      • Sanitizing the Input

      • PHP's Program Execution Functions

    • Summary

  • CHAPTER 11: PEAR

    • Popular PEAR Packages

      • Converting Numeral Formats

    • Installing and Updating PEAR

      • Installing PEAR

      • PEAR and Hosting Companies

      • Updating PEAR

    • Using the PEAR Package Manager

      • Viewing Installed Packages

      • Learning More About an Installed Package

      • Installing a Package

      • Using a Package

      • Upgrading a Package

      • Uninstalling a Package

      • Downgrading a Package

    • Summary

  • CHAPTER 12: Date and Time

    • The Unix Timestamp

    • PHP's Date and Time Library

    • Date Fu

      • Displaying the Localized Date and Time

      • Displaying the Web Page's Most Recent Modification Date

      • Determining the Number Days in the Current Month

      • Calculating the Date X Days from the Present Date

      • Creating a Calendar

    • PHP 5.1

      • Date Fundamentals

      • The Date Constructor

      • Accessors and Mutators

      • Validators

      • Manipulation Methods

    • Summary

  • CHAPTER 13: Forms and Navigational Cues

    • PHP and Web Forms

      • A Simple Example

      • Passing Form Data to a Function

      • Working with Multivalued Form Components

      • Generating Forms with PHP

      • Autoselecting Forms Data

      • PHP, Web Forms, and JavaScript

    • Navigational Cues

      • User-Friendly URLs

      • Breadcrumb Trails

      • Creating Custom Error Handlers

    • Summary

  • CHAPTER 14: Authentication

    • HTTP Authentication Concepts

    • PHP Authentication

      • Authentication Variables

      • Authentication Methodologies

    • User Login Administration

      • Password Designation

      • Testing Password Guessability with the CrackLib Library

      • One-Time URLs and Password Recovery

    • Summary

  • CHAPTER 15: Handling File Uploads

    • Uploading Files via the HTTP Protocol

    • Handling Uploads with PHP

      • PHP's File Upload/Resource Directives

      • The $_FILES Array

      • PHP's File-Upload Functions

      • Upload Error Messages

      • File-Upload Examples

    • Taking Advantage of PEAR: HTTP_Upload

      • Installing HTTP_Upload

      • Learning More About an Uploaded File

      • Moving an Uploaded File to the Final Destination

      • Uploading Multiple Files

    • Summary

  • CHAPTER 16: Networking

    • DNS, Services, and Servers

      • DNS

      • Services

      • Establishing Socket Connections

    • Mail

      • Configuration Directives

      • Sending a Plain-Text E-Mail

      • Sending an E-Mail with Additional Headers

      • Sending an E-Mail to Multiple Recipients

      • Sending an HTML-Formatted E-Mail

      • Sending an Attachment

    • IMAP, POP3, and NNTP

      • Requirements

      • Establishing and Closing a Connection

      • Learning More About Mailboxes and Mail

      • Retrieving Messages

      • Composing a Message

      • Sending a Message

      • Mailbox Administration

      • Message Administration

    • Streams

      • Stream Wrappers and Contexts

      • Stream Filters

    • Common Networking Tasks

      • Pinging a Server

      • A Port Scanner

      • Subnet Converter

      • Testing User Bandwidth

    • Summary

  • CHAPTER 17: PHP and LDAP

    • An Introduction to LDAP

      • Learning More About LDAP

    • Using LDAP from PHP

      • Connecting to the LDAP Server

      • Binding to the LDAP Server

      • Closing the LDAP Server Connection

      • Retrieving LDAP Data

      • Working with Entry Values

      • Counting Retrieved Entries

      • Retrieving Attributes

      • Sorting and Comparing LDAP Entries

      • Working with Entries

      • Deallocating Memory

      • Inserting LDAP Data

      • Updating LDAP Data

      • Deleting LDAP Data

      • Configuration Functions

      • Character Encoding

      • Working with the Distinguished Name

      • Error Handling

    • Summary

  • CHAPTER 18: Session Handlers

    • What Is Session Handling?

      • Cookies

      • URL Rewriting

      • The Session-Handling Process

    • Configuration Directives

    • Key Concepts

      • Starting a Session

      • Destroying a Session

      • Retrieving and Setting the Session ID

      • Creating and Deleting Session Variables

      • Encoding and Decoding Session Data

    • Practical Session-Handling Examples

      • Auto-Login

      • Recently Viewed Document Index

    • Creating Custom Session Handlers

      • Tying Custom Session Functions into PHP's Logic

      • Custom PostgreSQL-Based Session Handlers

    • Summary

  • CHAPTER 19: Templating with Smarty

    • What's a Templating Engine?

    • Introducing Smarty

    • Installing Smarty

    • Using Smarty

    • Smarty's Presentational Logic

      • Comments

      • Variable Modifiers

      • Control Structures

      • Statements

    • Creating Configuration Files

      • config_load

      • Referencing Configuration Variables

    • Using CSS in Conjunction with Smarty

    • Caching

      • Working with the Cache Lifetime

      • Eliminating Processing Overhead with is_cached()

      • Creating Multiple Caches per Template

      • Some Final Words About Caching

    • Summary

  • CHAPTER 20: Web Services

    • Why Web Services?

    • Real Simple Syndication

      • RSS Syntax

      • MagpieRSS

    • SimpleXML

      • SimpleXML Functions

      • SimpleXML Methods

    • SOAP

      • NuSOAP

      • PHP 5's SOAP Extension

    • Using a C# Client with a PHP Web Service

    • Summary

  • CHAPTER 21: Secure PHP Programming

    • Configuring PHP Securely

      • Safe Mode

      • Other Security-Related Configuration Parameters

    • Hiding Configuration Details

      • Hiding Apache and PHP

    • Hiding Sensitive Data

      • Take Heed of the Document Root

      • Denying Access to Certain File Extensions

    • Sanitizing User Data

      • File Deletion

      • Cross-Site Scripting

      • Sanitizing User Input: The Solution

    • Data Encryption

      • PHP's Encryption Functions

      • mhash

      • MCrypt

    • Summary

  • CHAPTER 22: SQLite

    • Introduction to SQLite

      • Installing SQLite

      • Using the SQLite Command-Line Interface

    • PHP's SQLite Library

      • SQLite Directives

      • Opening a Connection

      • Creating a Table in Memory

      • Closing a Connection

      • Querying a Database

      • Parsing Result Sets

      • Retrieving Result Set Details

      • Manipulating the Result Set Pointer

      • Learning More About Table Schemas

      • Working with Binary Data

      • Creating and Overriding SQLite Functions

      • Creating Aggregate Functions

    • Summary

  • CHAPTER 23: Introducing PDO

    • Another Database Abstraction Layer?

    • Using PDO

      • Installing PDO

      • PDO's Database Support

      • Connecting to a Database Server and Selecting a Database

      • Getting and Setting Attributes

      • Error Handling

      • Query Execution

      • Prepared Statements

      • Retrieving Data

      • Setting Bound Columns

      • Transactions

    • Summary

  • CHAPTER 24: Introducing PostgreSQL

    • PostgreSQL's Key Features

      • Data Integrity

      • Highly Scalable

      • Feature-Complete

      • Extensible

      • Platform Support

      • Flexible Security Options

      • Global Development, Local Flavor

      • Hassle-Free Licensing

      • Multiple Support Avenues

    • Real-World Users

      • Afilias Inc

      • The National Weather Service

      • WhitePages.com

    • Summary

  • CHAPTER 25: Installing PostgreSQL

    • PostgreSQL Licensing Requirements

    • Downloading PostgreSQL

      • Downloading the Unix Version

      • Downloading the Windows Version

      • Downloading the Documentation

    • Installing PostgreSQL

      • Installing PostgreSQL on Linux and Unix

      • Installing PostgreSQL on Windows 2000, XP, and 2003

      • Installing PostgreSQL on Windows 95, 98, and ME

    • Starting PostgreSQL for the First Time

    • Summary

  • CHAPTER 26: PostgreSQL Administration

    • Starting and Stopping the Server

    • Tuning Your PostgreSQL Installation

    • Working with Tablespaces

    • Vacuum and Analyze

    • Autovacuum

    • Backup and Recovery

    • Upgrading Between Versions

    • Summary

  • CHAPTER 27: The Many PostgreSQL Clients

    • What Is psql?

      • psql Options

    • Commonplace psql Tasks

      • Logging Onto and Off the Server

      • psql Commands

      • Storing psql Variables and Options

      • Learning More About Supported SQL Commands

      • Executing a Query

      • Modifying the psql Prompt

      • Controlling the Command History

    • GUI-based Clients

      • pgAdmin III

      • phpPgAdmin

      • Navicat

    • Summary

  • CHAPTER 28: From Databases to Datatypes

    • Working with Databases

      • Default Databases

      • Creating a Database

      • Connecting to a Database

      • Deleting a Database

      • Modifying Existing Databases

    • Working with Schemas

      • Creating Schemas

      • Altering Schemas

      • Dropping Schemas

      • The Schema Search Path

    • Working with Tables

      • Creating a Table

      • Copying a Table

      • Creating a Temporary Table

      • Viewing a Database's Available Tables

      • Viewing Table Structure

      • Deleting a Table

    • Altering a Table Structure

    • Working with Sequences

      • Creating a Sequence

      • Modifying Sequences

      • Sequence Functions

      • Deleting a Sequence

    • Datatypes and Attributes

      • Datatypes

      • Datatype Attributes

    • Composite Datatypes

      • Creating Composite Types

      • Altering Composite Types

      • Dropping Composite Types

    • Working with Domains

      • Creating Domains

      • Altering Domains

      • Dropping Domains

    • Summary

  • CHAPTER 29: Securing PostgreSQL

    • What You Should Do First

    • Securing the PostgreSQL Daemon

    • The PostgreSQL Access Privilege System

      • How the Privilege System Works

      • Where Is Access Information Stored?

      • User and Privilege Management

      • Secure PostgreSQL Connections

      • Summary

  • CHAPTER 30: PHP's PostgreSQL Functionality

    • Prerequisites

      • Enabling PHP's PostgreSQL Extension

      • PHP's PostgreSQL Configuration Directives

      • Sample Data

    • PHP's PostgreSQL Commands

      • Establishing and Closing a Connection

    • Queries

      • Query Execution

    • Retrieving Status and Error Information

      • Recuperating Query Memory

      • Retrieving and Displaying Data

      • Rows Selected and Rows Affected

    • Inserting, Modifying, and Deleting Data

      • Inserting Data

      • Mass Inserts

      • Modifying Data

      • Deleting Data

    • Prepared Statements

    • The Information Schema

    • Summary

  • CHAPTER 31: Practical Database Queries

    • Sample Data

    • Creating a PostgreSQL Database Class

      • Why Use the PostgreSQL Database Class?

      • Executing a Simple Query

      • Retrieving Multiple Rows

      • Counting Queries

      • Tabular Output

      • Linking to a Detailed View

      • Sorting Output

      • Creating Paged Output

      • Listing Page Numbers

    • Summary

  • CHAPTER 32: Views and Rules

    • Working with Views

    • The PostgreSQL Rule System

      • Working with Rules

      • Rule Types

    • Making Views Interactive

    • Working with Views from Within PHP

    • Summary

  • CHAPTER 33: PostgreSQL Functions

    • Operators

      • Logical Operators

      • Comparison Operators

      • Mathematical Operators

      • String Operators

      • Operator Precedence

    • Internal Functions

      • Date and Time Functions

      • String Functions

      • Aggregate Functions

      • Conditional Expressions

      • More Functions

    • User-Defined Functions

      • Create Function Syntax

      • SQL-Based Functions

      • PL/pgSQL-Based Functions

      • Other Procedural Languages

    • Summary

  • CHAPTER 34: PostgreSQL Triggers

    • What Is a Trigger?

      • Adding Triggers

      • Modifying Triggers

      • Removing Triggers

    • Writing Trigger Functions

      • Example Trigger Functions

      • Viewing Existing Triggers

    • Summary

  • CHAPTER 35: Indexes and Searching

    • Database Indexing

      • Primary Key Indexes

      • Unique Indexes

      • Normal Indexes

      • Full-Text Indexes

      • Indexing Best Practices

    • Forms-Based Searches

      • Performing a Simple Search

      • Extending Search Capabilities

      • Performing a Full-Text Search

    • Summary

  • CHAPTER 36: Transactions

    • What's a Transaction?

    • PostgreSQL’s Transactional Capabilities

      • Transaction Isolation

      • Sample Project

      • A Simple Example

      • Transaction Usage Tips

    • Building Transactional Applications with PHP

      • Beware of pg_query()

      • The Swap Meet Revisited

    • Summary

  • CHAPTER 37: Importing and Exporting Data

    • The COPY Command

      • Copying Data to and from a Table

      • Calling COPY from a PHP Script

    • Importing and Exporting Data with phpPgAdmin

    • Summary

  • INDEX

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    • I

    • J

    • K

    • L

    • M

    • N

    • O

    • P

    • Q

    • R

    • S

    • T

    • U

    • V

    • W

    • X

    • Y

    • Z

Nội dung

www.it-ebooks.info Beginning PHP and PostgreSQL 8 From Novice to Professional ■■■ W. Jason Gilmore and Robert H. Treat www.it-ebooks.info Beginning PHP and PostgreSQL 8: From Novice to Professional Copyright © 2006 by W. Jason Gilmore All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-547-3 ISBN-10 (pbk): 1-59059-547-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Matthew Moodie Technical Reviewers: Greg Sabino Mullane, Matt Wade Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Matt Wade Project Manager: Beth Christmas Copy Edit Manager: Nicole LeClerc Copy Editor: Bill McManus Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Cheu Compositor: Susan Glinert Stevens Proofreader: Nancy Sixsmith Indexer: John Collin Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code section. www.it-ebooks.info This book is dedicated to the memory of my grandfather, William J. Gilmore, for introducing me to the world of computers. —W. Jason Gilmore I dedicate this book to my mother, Gladys Emilia Treat. Puedes vivir solo una vez, pero si lo haces bien, una vez es suficiente. —Robert H. Treat www.it-ebooks.info v Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi ■CHAPTER 1 An Introduction to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 Installing and Configuring Apache and PHP . . . . . . . . . . . . . . . . . . . . 9 ■CHAPTER 3 PHP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 ■CHAPTER 4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 ■CHAPTER 5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 ■CHAPTER 6 Object-Oriented PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 ■CHAPTER 7 Advanced OOP Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 ■CHAPTER 8 Error and Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 ■CHAPTER 9 Strings and Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 ■CHAPTER 10 Working with the File and Operating System . . . . . . . . . . . . . . . . . 229 ■CHAPTER 11 PEAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 ■CHAPTER 12 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 ■CHAPTER 13 Forms and Navigational Cues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 ■CHAPTER 14 Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 ■CHAPTER 15 Handling File Uploads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 ■CHAPTER 16 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 ■CHAPTER 17 PHP and LDAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 ■CHAPTER 18 Session Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 ■CHAPTER 19 Templating with Smarty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 ■CHAPTER 20 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 ■CHAPTER 21 Secure PHP Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 ■CHAPTER 22 SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 www.it-ebooks.info vi ■CHAPTER 23 Introducing PDO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 ■CHAPTER 24 Introducing PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 ■CHAPTER 25 Installing PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 ■CHAPTER 26 PostgreSQL Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593 ■CHAPTER 27 The Many PostgreSQL Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611 ■CHAPTER 28 From Databases to Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625 ■CHAPTER 29 Securing PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649 ■CHAPTER 30 PHP’s PostgreSQL Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 ■CHAPTER 31 Practical Database Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 ■CHAPTER 32 Views and Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 ■CHAPTER 33 PostgreSQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719 ■CHAPTER 34 PostgreSQL Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739 ■CHAPTER 35 Indexes and Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749 ■CHAPTER 36 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765 ■CHAPTER 37 Importing and Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 www.it-ebooks.info vii Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi ■CHAPTER 1 An Introduction to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 PHP 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 PHP 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 General Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Practicality. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Possibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 ■CHAPTER 2 Installing and Configuring Apache and PHP . . . . . . . . . . . . . . 9 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Obtaining the Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 The Installation Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Testing Your Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Customizing the Unix Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Customizing the Windows Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Viewing and Downloading the Documentation . . . . . . . . . . . . . . . . . 18 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Managing PHP’s Configuration Directives . . . . . . . . . . . . . . . . . . . . . 19 PHP’s Configuration Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Contents www.it-ebooks.info viii ■CONTENTS ■CHAPTER 3 PHP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Escaping to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Default Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Short-Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 ASP-Style. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Embedding Multiple Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Single-line C++ Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Shell Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Multiple-Line C Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 print() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 echo() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 printf(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 sprintf(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Scalar Datatypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Compound Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Special Datatypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Type Juggling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Type-Related Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Type Identifier Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 PHP’s Superglobal Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Variable Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 String Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Double Quotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Single Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Heredoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 www.it-ebooks.info ■CONTENTS ix Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Execution Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Looping Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 File Inclusion Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 ■CHAPTER 4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Invoking a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Creating a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Passing Arguments by Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Passing Arguments by Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Default Argument Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Returning Values from a Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Nesting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Variable Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Function Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 ■CHAPTER 5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Outputting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Testing for an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Adding and Removing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Locating Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Traversing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Determining Array Size and Uniqueness . . . . . . . . . . . . . . . . . . . . . . . . . 116 Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Merging, Slicing, Splicing, and Dissecting Arrays . . . . . . . . . . . . . . . . . . 124 Other Useful Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 www.it-ebooks.info x ■CONTENTS ■CHAPTER 6 Object-Oriented PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 The Benefits of OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Key OOP Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 __set() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Type Hinting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 The instanceof Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Helper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Autoloading Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 ■CHAPTER 7 Advanced OOP Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Advanced OOP Features Not Supported by PHP . . . . . . . . . . . . . . . . . . . 157 Object Cloning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Cloning Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 The __clone() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Inheritance and Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Implementing a Single Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Implementing Multiple Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 www.it-ebooks.info [...]... 581 Installing PostgreSQL on Linux and Unix 582 Installing PostgreSQL on Windows 2000, XP, and 2003 585 Installing PostgreSQL on Windows 95, 98, and ME 589 Starting PostgreSQL for the First Time 589 Summary 591 ■CHAPTER 26 PostgreSQL Administration ... together, PHP and PostgreSQL offer users an impressive platform for building high-powered Web applications This book shows you how Beginning PHP and PostgreSQL 8: From Novice to Professional helps you sort the substantive from the superfluous to begin creating PHP- and PostgreSQL- driven Web applications as quickly as possible Based on the structure and material found in the bestselling title Beginning PHP. .. ■CHAPTER 8 Error and Exception Handling 177 Configuration Directives 177 Error Logging 180 Exception Handling 183 Why Exception Handling Is Handy 183 PHP s Exception-Handling Implementation 185 Summary... ■CHAPTER 37 Importing and Exporting Data 777 The COPY Command 777 Copying Data to and from a Table 7 78 Calling COPY from a PHP Script 782 Importing and Exporting Data with phpPgAdmin 783 Summary 785 ■INDEX ... mailing lists and Web sites, has spoken twice at OSCon on PostgreSQL topics, and has contributed code to the PostgreSQL core He is the primary developer of the DBD::Pg module, and has been recognized as a PostgreSQL Major Developer for all of PostgreSQL work He has a strong interest in PGP and cryptography, and attends keysignings as often as possible His PGP fingerprint is 2529 DF6A B8F7 9407 E944... 6 78 Retrieving and Displaying Data 6 78 Rows Selected and Rows Affected 681 Inserting, Modifying, and Deleting Data 682 Inserting Data 682 Mass Inserts 683 Modifying Data 684 Deleting... of a better understanding of both the software’s behaviors and quirks So grab a snack and cozy up to your keyboard; it’s time to build www.it-ebooks.info 7 CHAPTER 2 ■■■ Installing and Configuring Apache and PHP I n this chapter, you’ll learn how to install and configure PHP, and in the process learn how to install the Apache Web server If you don’t already have a working Apache /PHP server at your... 2529 DF6A B8F7 9407 E944 45B4 BC9B 9067 1496 4AC8, and he has been known to sneak it into code he has written He currently works as a software developer for End Point, primarily doing PostgreSQL, Perl, and PHP work He and his wife Joy enjoy traveling, and try to make at least one overseas trip a year ■ MATT WADE is a database analyst by day and a freelance PHP developer by night He has extensive experience... Displaying the Localized Date and Time 279 Displaying the Web Page’s Most Recent Modification Date 283 Determining the Number Days in the Current Month 283 Calculating the Date X Days from the Present Date 284 Creating a Calendar 285 PHP 5.1 288 Date Fundamentals ... user, developer, and advocate He has worked with a number of projects but his favorite is certainly PostgreSQL His current involvement includes helping maintain the postgresql. org Web sites, working on phpPgAdmin, and contributing to the PostgreSQL core whenever he can He has contributed several articles to the PostgreSQL “techdocs” site, presented multiple times at OSCon, worked as the PHP Foundry Admin . www.it-ebooks.info Beginning PHP and PostgreSQL 8 From Novice to Professional ■■■ W. Jason Gilmore and Robert H. Treat www.it-ebooks.info Beginning PHP and PostgreSQL 8: From Novice to Professional Copyright. . . . . 581 Installing PostgreSQL on Linux and Unix . . . . . . . . . . . . . . . . . . . . . 582 Installing PostgreSQL on Windows 2000, XP, and 2003 . . . . . . . . 585 Installing PostgreSQL. . . . . . . . . . . . . . 180 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Why Exception Handling Is Handy. . . . . . . . . . .

Ngày đăng: 24/04/2014, 14:41

TỪ KHÓA LIÊN QUAN