Web database application with php mysql 2nd edition


















There are many reasons for serving up dynamic content from a web site: to offer an online shopping site, create customized information pages for users, or just manage a large volume of content through a database. This book gives you the background and tools to do the job safely and reliably. It goes into detail on such practical issues as validating input do you know what a proper credit card number looks like? But this book goes even further. It shows how JavaScript and PHP can be used in tandem to make a user's experience faster and more pleasant.

It shows the correct way to handle errors in user input so that a site looks professional. It introduces the vast collection of powerful tools available in the PEAR repository and shows how to use some of the most popular tools. Even while it serves as an introduction to new programmers, the book does not omit critical tasks that web sites require.

For instance, every site that allows updates must handle the possibility of multiple users accessing data at the same time. This book explains how to solve the problem in detail with locking.

Through a sophisticated sample application--Hugh and Dave's Wine Store--all the important techniques of dynamic content are introduced. Good design is emphasized, such as dividing logic from presentation. This new edition has been redesigned around the rich offerings of PEAR. Several of these, including the Template package and the database-independent query API, are fully integrated into examples and thoroughly described in the text.

Topics include:. Learn algorithms for solving classic computer science problems with this concise guide covering everything from fundamental …. Distributed systems have become more fine-grained as organizations shift from code-heavy monolithic applications to smaller, self-contained …. Today, software engineers need to know not only how to program effectively but also how to ….

Without established design patterns to guide them, developers have had to build distributed systems from scratch, …. Skip to main content. Start your free trial. Williams , David Lane. Buy on Amazon. Book description There are many reasons for serving up dynamic content from a web site: to offer an online shopping site, create customized information pages for users, or just manage a large volume of content through a database.

Show and hide more. Table of contents Product information. Database Applications and the Web 1. The Web 1. Three-Tier Architectures 1. HTTP example 1. State 1. Thickening the Client in the Three-Tier Model 1. The Middle Tier 1.

Web servers 1. Web Scripting with PHP 1. Introducing PHP5 1. The Database Tier 1. Database Management Systems 1. SQL 1. Why use a database server? Examples of when to use a database server 1. Examples of when not to use a DBMS 1. The MySQL server 1. Introducing MySQL 4 2. Introducing PHP 2. PHP Basics 2. Creating PHP scripts 2. Comments 2. Outputting data with echo and print 2.

String Literals 2. Variable substitution 2. Character encoding 2. Variables 2. Types 2. Constants 2. Expressions, Operators, and Variable Assignment 2.

Operator precedence 2. Conditions and Branches 2. Conditional Expressions 2. Loops 2. Changing Loop Behavior 2. Functions 2.

Working with Types 2. Type Conversion 2. Automatic Type Conversion 2. Examining Variable Type and Content 2. Is-identical and is-not-identical operators 2. Testing, setting, and unsetting variables 2. User-Defined Functions 2.

Parameter Types and Return Types 2. Variable Scope 2. Global variables 2. Static variables 2. Passing Variables to Functions 2. Passing parameters by reference 2. Assigning by reference 2. Default parameter values 2. Reusing Functions with Include and Require Files 2. Managing include files 2. A Working Example 3. Arrays 3. Creating Arrays 3.

Associative arrays 3. Removing elements from an array 3. Array order 3. Heterogeneous arrays 3. Multidimensional arrays 3. Using foreach Loops with Arrays 3. Basic Array Functions 3. Counting elements in arrays 3. Functions that create arrays 3. Exploding and imploding strings 3. Finding the maximum and minimum values in an array 3. Keys and values 3. Joining two or more arrays 3. Sorting Arrays 3.

Sorting with sort and rsort 3. Sorting associative arrays 3. Sorting on keys 3. Sorting with user-defined element comparison 3. Strings 3. Length of a String 3. Printing and Formatting Strings 3. Creating formatted output with sprintf and printf 3.

Padding strings 3. Changing case 3. Trimming whitespace 3. Comparing Strings 3. Finding and Extracting Substrings 3. Extracting a substring from a string 3. Finding the position of a substring 3. Extracting a found portion of a string 3. Replacing Characters and Substrings 3. Replacing substrings 3. Translating characters and substrings 3. Regular Expressions 3. Regular Expression Syntax 3. Characters and wildcards 3.

Character lists 3. Anchors 3. Optional and repeating characters 3. Groups 3. Alternative patterns 3. Escaping special characters 3. Metacharacters 3. Regular Expression Functions 3. Finding and extracting values 3.

Splitting a string into an array 3. Dates and Times 3. Generating a Timestamp 3. Current time 3. Creating timestamps with mktime and gmmktime 3. String to timestamp 3. Subsecond times 3. Formatting a Date 3. Validating a Date 3. Integers and Floats 3. Absolute Value 3. Ceiling and Floor 3. Rounding 3. Number Systems 3. Basic Trigonometry Functions 3. Powers and Logs 3. Testing Number Results 3. Random Number Generation 4. Classes and Objects 4.

Member Variables 4. Member Functions 4. Using include Files for Class Definitions 4. Constructors 4. Destructors 4. Private Members Variables 4. Private Member Functions 4. Static Member Variables 4. Static Member Functions 4. Cloning Objects 4. Cloning in PHP5 4. Cloning in PHP4 4. Inheritance 4. Calling Parent Constructors 4.

Redefined Functions 4. Protected Member Variables and Functions 4. Final Functions 4. Throwing and Catching Exceptions 4. The Exception Class 5. Database Basics 5. Introducing Relational Databases 5. Database Terminology 5. The Winestore Database 5. The winestore entity-relationship model 5. Managing Databases and Tables 5. Creating Databases 5. Creating Tables 5. Tables and attributes 5. Modifiers 5. Keys 5. Deleting Databases and Tables 5.

Inserting, Updating, and Deleting Data 5. Inserting Data 5. Defaults 5. Auto-increment 5. Deleting Data 5. Updating Data 5. Basic Querying 5. Sorting and Grouping Output 5. Combining clauses 5. Join Queries 5. Beware of the Cartesian Product 5. Elementary Natural Joins 5. Examples 5. Joins with More than Two Tables 5. Case Study: Adding a New Wine 6. Querying Web Databases 6. Opening and Using a Database Connection 6. Working with Table Structures 6. Formatting Results 6.

Using Require Files in Practice 6. Case Study: Producing a Select List 6. Implementing the selectDistinct function 6. Processing User Input 6. Passing Data from the Browser to the Server 6. Passing Data with URLs 6.

Passing Data with Embedded Links 6. More on Accessing User Data 6. Before PHP 4. Processing Form Data 6. Other form issues 6. Security and User Data 6. Querying with User Input 6.

One-Component Querying 6. Frequently Used Functions 6. Other Functions 6. Functions to Avoid 7. PEAR 7. Overview 7. Core Components 7. Unix systems—PHP 4. Microsoft Windows—PHP 4. Getting started 7. Packages 7. Installing, Upgrading, and Understanding Packages 7. Finding out about packages 7. Using the PEAR installer 7. Working with blocks and placeholders 7. Nested blocks 7. Preserving and removing blocks 7. More on nesting and optional blocks 7.

Optional Packages 7. Authentication 7. Benchmarking 7. Caching 7. Console 7. Database 7. Date 7. Filesystem 7. HTML 7. HTTP 7. Internationalization 7. Logging 7. Mail 7. Networking 7. PHP 7. XML 7. Web services 8. Writing to Web Databases 8. Database Inserts, Updates, and Deletes 8. Reloading Data and Relocation Techniques 8. Solving the reload problem in practice 8. Inserting, Updating, and Deleting Data 8.

Inserting data 8. Updating data 8. Case study: updates in practice 8. Deleting data 8. Issues in Writing Data to Databases 8. Transactions and Concurrency 8. When and how to lock tables 8. Locking for performance 8. Locking Tables in Web Database Applications 8. Locking with an auxiliary table 8. Validation and Error Reporting Principles 9. Models That Do Work 9. Mandatory Data 9.

Validating Strings 9. Basic techniques 9. Validating Zip and postcodes 9. Validating email addresses 9. Validating URLs 9. Validating numbers 9. Validating credit cards 9. Validating Dates and Times 9. Dates 9. Times 9. Logic, the date function, and MySQL 9. JavaScript and Client-Side Validation 9. Introducing JavaScript 9.

JavaScript and PHP 9. Generating output 9. Loops and conditionals 9. Functions 9. Debugging JavaScript 9. Objects 9. Events 9. Methods and properties 9. JavaScript Examples 9. A password form validation function 9. Rollover presentation with mouseover events 9. Prefilling form data with JavaScript calculations 9. Interacting with the web browser 9. Which browser is the user using? Drop-down menus 9. The JavaScript validation script 9. Using the JavaScript validation function 9.

The PHP and template components Sessions You can thicken the client tier to put more work on the browser. Using popular technologies such as Java, JavaScript, and Macromedia Flash, you can develop application components that process data independently of the web server or preprocess data before sending it to the server. The middle tier has many roles in a web database application.

It brings together the other tiers, drives the structure and content of the data displayed to the user, provides security and authentication, and adds state to the application. There are essentially two types of request made to a web server: the first asks for a file—often a static HTML web page or an image—to be returned, and the second asks for a program or script to be run and its output to be returned.

Apache is an open source, fast, and scalable web server. It can handle simultaneous requests from browsers and is designed to run under multitasking operating systems such as Linux, Mac OS X, and Microsoft Windows.

It has low resource requirements, can effectively handle changes in request loads, and can run fast on even modest hardware. It is widely used and tested. The current release at the time of writing is 2. The other server programs notify their availability to handle requests to the coordinating server. If too few servers are available to handle incoming requests, the coordinating server may start new servers; if too many are free, it may kill spare servers to save resources.

The server administrator controls the behavior of Apache through more than directives that affect resource requirements, response time, flexibility in dealing with request load variability, security, how HTTP requests are handled and logged, how scripting engines are used to run scripts, and most other aspects of its operation.

The configuration of Apache for most web database applications is straightforward. More details on Apache configuration can be found in the resources listed in Appendix G. PHP is the most widely supported and used web scripting language and an excellent tool for building web database applications. Community efforts to maintain and improve it are unconstrained by commercial imperatives.

On the downside, this can blend the scripts with the presentation; however the template techniques described in Chapter 7 can solve most of these problems. It is a fully featured object-oriented programming language, with more than libraries of programming functions for tasks as diverse as math, sorting, creating PDF documents, and sending email.

There are over 15 libraries for native, fast access to the database tier. Using its built-in Zend scripting engine, PHP script execution is fast and all components run within the main memory space of PHP in contrast to other scripting frameworks, in which components are in distinct modules. Our experiments suggest that for tasks of at least moderate complexity, PHP is faster than other popular scripting tools. Apache and PHP run on many different platforms and operating systems.

PHP can also be integrated with other web servers. At the time of writing, PHP4 Version 4. The scripts in this book have been developed and tested using PHP4, and testing on PHP5 has identified a few limitations.

When a feature is only available in PHP5, we tell you in the text. PHP is a major topic of this book. PHP libraries that are important to web database application development are the subject of Chapter 6 and Chapter 8 through Chapter Other pointers to web resources, books, and commercial products for PHP development are listed in Appendix G.

A technical explanation of the new features of PHP5 is presented in the next section. PHP4 included the first release of the Zend engine version 1. PHP5 includes a new Zend engine version 2. In brief, the following are the major new features in PHP5. Many of these features are explained in detail elsewhere in this book:. All of these features are available in PHP5. Handles to objects are now passed, rather than the objects themselves. This has substantially improved the performance of PHP.

Support for non-Western character sets and Unicode. This is discussed in Chapter 3. New try These are discussed in Chapter 4. This feature has been back-ported into PHP4 and is discussed in Chapter These tools have been replaced with a single new, robust framework in PHP5. The library has the significant feature that it allows an SQL query to be prepared once, and executed many times, and this substantially improves speed if a query is often used.

This library is briefly described in Chapter 6 , and is the source of many of the PHP4 and PHP5 compatibility problems described throughout in this book. The database tier stores and retrieves data. Importantly, a good database tier must allow quick and flexible access to millions upon millions of facts. Managing data in the database tier requires complex software. Fortunately, most database management systems DBMSs or servers are designed so that the software complexities are hidden.

To effectively use a database server, skills are required to design a database and formulate queries using the SQL language; SQL is discussed in Chapter 5. An understanding of the underlying architecture of the database server is unimportant to most users. In this book, we use the MySQL server to manage data. However, there are downsides to MySQL that we discuss later in this section.

The first step in successful web database application development is understanding system requirements and designing databases. We discuss techniques for modeling system requirements, converting a model into a database, and the principles of database technology in Appendix E. In this section, we focus on the database tier and introduce database software by contrasting it with other techniques for storing data. Chapter 5 and Chapter 15 cover the standards and software we use in more detail.

There are other server choices for storing data in the database tier. These include search engines, document management systems, and gateway services such as email software.

Our discussions in this book focus on the MySQL server in the database tier. A database is a collection of related data, and an application can have more than one database. A database might contain a few entries that make up a simple address book of names, addresses, and phone numbers.

At the other extreme, a database can contain tens or hundreds of millions of records that describe the catalog, purchases, orders, and payroll of a large company. Most web database applications have small- to medium-size databases that store thousands, or tens of thousands, of records.

Database servers are complex software. For all but the largest applications, understanding and configuring the internals of a database server is usually unnecessary. The database server applications interface is accessed using SQL. SQL has had a complicated life. IBM published a different standard one year later!

Consider an SQL example. Suppose you want to store information about books in a library. These are only some of the features of SQL, and even these features can be used in complex ways. SQL also allows you to update and delete data and databases, and it includes many other features such as security and access management, multiuser transactions that allow many users to access the same database without corrupting the data, tools to import and export data, and powerful undo and redo features.

SQL is discussed in detail in Chapter 5 and Chapter Why use a complex database server to manage data? There are several reasons that can be explained by contrasting a database with a spreadsheet, a simple text file, or a custom-built method of storing data.

A few example situations where a database server should and should not be used are discussed later in this section. Take spreadsheets as an example. Spreadsheet worksheets are typically designed for a specific application. If two users store names and addresses, they are likely to organize data in a different way and develop custom methods to move around and summarize the data. In contrast, a database server and SQL provide data-program independence, where the method for storing the data is independent of the language that accesses it.

Managing complex relationships is difficult in a spreadsheet or text file. If we want to store information about the purchases by our customers, the spreadsheet becomes wider still, and problems start to emerge. For example, it is difficult to determine the maximum number of columns needed to store orders and to design a method to process these for reporting.

In contrast, databases are designed to manage complex relational data. A database server usually permits multiple users to access a database at the same time in a methodical way. At best, a shared spreadsheet or text file permits very limited concurrent access. An additional benefit of a database server is its speed and scalability.

In many cases, searching a spreadsheet or a special-purpose file might be perfectly acceptable, or even faster if it is designed carefully and the volume of data is small. However, for managing large amounts of related information, the underlying search structures allow fast searching, and if information needs are complex, a database server should optimize the method of retrieving the data.

There are also other advantages of database servers, including data-oriented and user-oriented security, administration software, portability, and data recovery support. A practical benefit of this is reduced application development time: the system is already built, it needs only data and queries to access the data. In any of these situations, a database server should be used to manage data:.

There is at least a moderate amount of data. For example, you might need to maintain information about a few hundred customers. There are relationships between the stored data items. For example, customers may have any number of related invoices. There is more than one kind of data object. For example, there might be information about customers, orders, inventory, and other data in an online store. There are constraints that must be rigidly enforced on the data, such as field lengths, field types, uniqueness of customer numbers, and so on.

New or consolidated information must be produced from basic, related information; that is, the data must be queried to produce reports or results. Security is important. There is a need to enforce rules as to who can access the data. There are some situations where a relational DBMS is probably unnecessary or unsuitable. Here are some examples:. For example, if a log entry is written when a user logs in and logs out, appending the entry to the end of a simple text file may be sufficient.

The data management task is trivial and accessing a database server adds unnecessary overhead. In this case, the data might be coded into a web script in the middle tier. MySQL has most of the features of high-end commercial database servers, including the ability to manage very large quantities of data. Its design is ideally suited to managing databases that are typical of most web database applications. The current version at the time of writing is MySQL 4.

Contrary to popular belief, since , MySQL has supported nested queries, transactions, and row or record locking. MySQL is another major topic of this book. A technical explanation of the features of MySQL 4 is presented in the next section. The current version, MySQL 4.



0コメント

  • 1000 / 1000