Docsity

Build your website with php and MySQL, Assignments of Computer Science

Toggle sidebar
bg1
SITEPOINT BOOKS
Advocate best practice techniques
Lead you through practical examples
Provide working code for your web site
Make learning easy and fun
Detailed installation instructions
Easy-to-understand diagrams
Practical code examples
WHAT’S INSIDE?
PANTONE 2955 CPANTONE Orange 021 C
CMYK 100, 45, 0, 37CMYK O, 53, 100, 0
Black 100%Black 50%
CMYK:
Pantone:
Grey scale
PANTONE 2955 CPANTONE Orange 021 C
CMYK 100, 45, 0, 37CMYK O, 53, 100, 0
Black 100%Black 50%
CMYK:
Pantone:
Grey scale
Visit us on the Web at sitepoint.com or for sales and support email [email protected]
ISBN: 978-0-9805768-1-8
ALL SOURCE CODE AVAILABLE FOR DOWNLOAD
LEARNING PHP & MYSQL HAS NEVER BEEN SO EASY!
Kevin Yank is a world-renowned leader in web development. When not
writing best sellers, Kevin is the Technical Director of sitepoint.com and editor
of the popular SitePoint Tech Times newsletter. Kevin has also co-authored
Simply JavaScript and Everything You Know About CSS Is Wrong!
ABOUT KEVIN YANK
BUILD YOUR OWN
DATABASE
DRIVEN WEB SITE
USING PHP & MYSQL
TEACH YOURSELF PHP & MYSQL
THE EASY WAY ...
Build Your Own Database Driven Web Site Using PHP & MySQL
is a practical hands-on guide to learning all the tools, principles,
and techniques needed to build a fully functional database driven
web site using PHP & MySQL. This book covers everything from
installing PHP and MySQL on Windows, Linux, and Mac computers
through to building a live, web-based content management system.
You’ll learn how to:
Install PHP 5 & MySQL 5 on Windows, Linux, or Mac OS X
Gain a thorough understanding of PHP syntax
Master database design principles and SQL
Build a working content management system
Add, edit, and delete web content without using HTML
Build an ecommerce shopping cart
Utilize sessions and cookies to track site visitors
Craft SEO-friendly and memorable URLs
And a whole lot more ...
BY KEVIN YANK
4TH EDITION
PHP & MYSQL
YANK
BUILD YOUR OWN
DATABASE DRIVEN WEB SITE
USING PHP & MYSQL
USD $39.95
WEB PROGRAMMING
CAD $49.95
phpmysql4.indd 1 5/28/2009 5:51:24 PM
Licensed to [email protected]
www.it-ebooks.info
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Build your website with php and MySQL and more Assignments Computer Science in PDF only on Docsity!

PANTONE Orange 021 C PANTONE 2955 C CMYK O, 53, 100, 0 CMYK 100, 45, 0, 37 Black 50% Black 100%

CMYK:

Pantone:

Grey scale

LEARNING PHP & MYSQL HAS NEVER BEEN SO EASY!

BUILD YOUR OWN

DATABASE

DRIVEN WEB SITE

USING PHP & MYSQL

BY KEVIN YANK

4 TH EDITION

Summary of Contents

Build Your Own Database Driven Web Site Using PHP & MySQL

by Kevin Yank Copyright © 2009 SitePoint Pty. Ltd. Managing Editor : Chris Wyness Editor : Kelly Steele Technical Editor : Andrew Tetlaw Cover Design : Alex Walker Indexer : Russell Brooks Printing History : Latest Update : July 2009 1st Ed. Aug. 2001, 2nd Ed. Feb. 2003, 3rd Ed. Oct. 2004 Fourth Edition: July 2009

Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein.

Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark.

Published by SitePoint Pty. Ltd. 48 Cambridge Street Collingwood VIC Australia 3066. Web: www.sitepoint.com Email: [email protected] ISBN 978-0-9805768-1- Printed and bound in the United States of America

iv

About the Author

As Technical Director for SitePoint, Kevin Yank keeps abreast of all that is new and exciting in web technology. Best known for the book you are reading right now, he also co-authored Simply JavaScript (http://www.sitepoint.com/books/javascript1/) with Cameron Adams and Everything You Know About CSS Is Wrong! (http://www.sitepoint.com/books/csswrong1/ ) with Rachel Andrew. In addition, Kevin hosts the SitePoint Podcast (http://www.sitepoint.com/podcast/) and writes the SitePoint Tech Times , a free email newsletter that goes out to over 240,000 subscribers worldwide.

Kevin lives in Melbourne, Australia and enjoys speaking at conferences, as well as visiting friends and family in Canada. He’s also passionate about performing improvised comedy theater with Impro Melbourne (http://www.impromelbourne.com.au/) and flying light aircraft. Kevin’s personal blog is Yes, I’m Canadian (http://yesimcanadian.com/).

About the Technical Editor

Andrew Tetlaw has been tinkering with web sites as a web developer since 1997. At SitePoint he is dedicated to making the world a better place through the technical editing of SitePoint books, kits, articles, and newsletters. He is also a busy father of five, enjoys coffee, and often neglects his blog at http://tetlaw.id.au/.

About SitePoint

SitePoint specializes in publishing fun, practical, and easy-to-understand content for Web professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums.

v

To my parents, Cheryl and Richard, for making all this possible.

Chapter 4 Publishing MySQL Data on the

    1. Installation Preface xix
    1. Introducing MySQL
    1. Introducing PHP
    1. Publishing MySQL Data on the Web
    1. Relational Database Design
    1. Structured PHP Programming
    1. A Content Management System
    1. Content Formatting with Regular Expressions
    1. Cookies, Sessions, and Access Control
    1. MySQL Administration
    1. Advanced SQL Queries
    1. Binary Data
  • A. MySQL Syntax Reference
  • B. MySQL Functions
  • C. MySQL Column Types
  • D. PHP Functions for Working with MySQL
  • Index
  • Chapter 2 Introducing MySQL
  • An Introduction to Databases
  • Logging On to MySQL
  • Structured Query Language
  • Creating a Database
  • Creating a Table
  • Inserting Data into a Table
  • Viewing Stored Data
  • Modifying Stored Data
  • Deleting Stored Data
  • Let PHP Do the Typing
  • Chapter 3 Introducing PHP
  • Basic Syntax and Statements
  • Variables, Operators, and Comments
  • Arrays
  • User Interaction and Forms
  • Control Structures
  • Hiding the Seams
  • Avoid Advertising Your Technology Choices
  • Use PHP Templates
  • Many Templates, One Controller
  • Bring On the Database
  • Web
  • The Big Picture
  • Connecting to MySQL with PHP
  • Sending SQL Queries with PHP
  • Handling SELECT Result Sets
  • Inserting Data into the Database
  • Deleting Data from the Database
  • Mission Accomplished
  • Chapter 5 Relational Database Design
  • Giving Credit Where Credit is Due
  • Rule of Thumb: Keep Entities Separate
  • SELECT with Multiple Tables
  • Simple Relationships
  • Many-to-Many Relationships
  • One for Many, and Many for One
  • Chapter 6 Structured PHP Programming
  • Include Files
  • Including HTML Content
  • Including PHP Code
  • Types of Includes
  • Shared Include Files
  • Custom Functions and Function Libraries
  • Variable Scope and Global Access
  • Structure in Practice: Template Helpers
  • The Best Way
  • Chapter 7 A Content Management System
  • The Front Page
  • Managing Authors
  • Deleting Authors
  • Adding and Editing Authors
  • Managing Categories
  • Managing Jokes
  • Searching for Jokes
  • Adding and Editing Jokes
  • Deleting Jokes
  • Summary
  • Expressions Chapter 8 Content Formatting with Regular
  • Regular Expressions
  • String Replacement with Regular Expressions
  • Boldface and Italic Text
  • Paragraphs
  • Hyperlinks
  • Matching Tags
  • Putting It All Together
  • Real World Content Submission
  • Control Chapter 9 Cookies, Sessions, and Access
  • Cookies
  • PHP Sessions
  • A Simple Shopping Cart
  • Access Control
  • Database Design
  • Controller Code
  • Function Library
  • Managing Passwords and Roles
  • A Challenge: Joke Moderation
  • The Sky’s the Limit
  • Chapter 10 MySQL Administration
  • phpMyAdmin
  • Backing Up MySQL Databases
  • Database Backups Using mysqldump
  • Incremental Backups Using Binary Logs
  • MySQL Access Control
  • Granting Privileges
  • Revoking Privileges
  • Access Control Tips
  • Locked Out?
  • Checking and Repairing MySQL Data Files
  • Better Safe than Sorry
  • Chapter 11 Advanced SQL Queries
  • Sorting SELECT Query Results
  • Setting LIMITs
  • LOCKing TABLES
  • Column and Table Name Aliases
  • GROUPing SELECT Results
  • LEFT JOINs
  • Limiting Results with HAVING
  • Further Reading
  • Chapter 12 Binary Data
  • Semi-dynamic Pages
  • Handling File Uploads
  • Assigning Unique Filenames
  • Recording Uploaded Files in the Database
  • Binary Column Types
  • Storing Files
  • Viewing Stored Files
  • Putting It All Together
  • Large File Considerations
  • MySQL Packet Size
  • PHP Script Timeout
  • The End
  • Appendix A MySQL Syntax Reference
  • SQL Statements Implemented in MySQL
  • ALTER TABLE
  • ANALYZE TABLE
  • CREATE DATABASE
  • CREATE INDEX
  • CREATE TABLE
  • DELETE
  • DESCRIBE/DESC
  • DROP DATABASE
  • DROP INDEX
  • DROP TABLE
  • EXPLAIN
  • GRANT
  • INSERT
  • LOAD DATA INFILE
  • LOCK/UNLOCK TABLES
  • OPTIMIZE TABLE
  • RENAME TABLE
  • REPLACE
  • REVOKE
  • SELECT
  • SET
  • SHOW
  • TRUNCATE
  • UNLOCK TABLES
  • UPDATE
  • USE
  • Appendix B MySQL Functions
  • Control Flow Functions
  • Mathematical Functions
  • String Functions
  • Date and Time Functions
  • Miscellaneous Functions
  • Functions for Use with GROUP BY Clauses
  • Appendix C MySQL Column Types
  • Numerical Types
  • Character Types
  • Date/Time Types
  • MySQL Appendix D PHP Functions for Working with
  • Common PHP mysqli_* Functions
  • mysqli_affected_rows
  • mysqli_character_set_name
  • mysqli_close
  • mysqli_connect
  • mysqli_connect_errno
  • mysqli_connect_error
  • mysqli_data_seek
  • mysqli_errno
  • mysqli_error
  • mysqli_fetch_all
  • mysqli_fetch_array
  • mysqli_fetch_assoc
  • mysqli_fetch_field
  • mysqli_fetch_field_direct
  • mysqli_fetch_fields
  • mysqli_fetch_lengths
  • mysqli_fetch_object
  • mysqli_fetch_row
  • mysqli_field_count
  • mysqli_field_seek
  • mysqli_field_tell
  • mysqli_free_result
  • mysqli_get_client_info
  • mysqli_get_client_version
  • mysqli_get_host_info
  • mysqli_get_proto_info
  • mysqli_get_server_info
  • mysqli_get_server_version
  • mysqli_info
  • mysqli_insert_id
  • mysqli_num_fields
  • mysqli_num_rows
  • mysqli_ping
  • mysqli_query
  • mysqli_real_escape_string
  • mysqli_real_query
  • mysqli_select_db
  • mysqli_set_charset
  • mysqli_stat
  • mysqli_store_result
  • mysqli_thread_id
  • mysqli_use_result
  • Index

Preface

PHP and MySQL have changed.

Back in 2001, when I wrote the first edition of this book, readers were astonished to discover that you could create a site full of web pages without having to write a separate HTML file for each page. PHP stood out from the crowd of programming languages, mainly because it was easy enough for almost anyone to learn and free to download and install. The MySQL database, likewise, provided a simple and free solution to a problem that, up until that point, had been solvable only by expert programmers with corporate budgets.

Back then, PHP and MySQL were special—heck, they were downright miraculous! But over the years, they have gained plenty of fast-moving competition. In an age when anyone with a free WordPress^1 account can set up a full-featured blog in 30 seconds flat, it’s no longer enough for a programming language like PHP to be easy to learn; nor is it enough for a database like MySQL to be free.

Indeed, as you sit down to read this book, you probably have ambitions that extend beyond what you can throw together using the free point-and-click tools of the Web. You might even be thinking of building an exciting, new point-and-click tool of your own. WordPress, after all, is built using PHP and MySQL, so why limit your vision to anything less?

To keep up with the competition, and with the needs of more demanding projects, PHP and MySQL have had to evolve. PHP is now a far more intricate and powerful language than it was back in 2001, and MySQL is a vastly more complex and capable database. Learning PHP and MySQL today opens up a lot of doors that would have remained closed to the PHP and MySQL experts of 2001.

That’s the good news. The bad news is that, in the same way that a butter knife is easier to figure out than a Swiss Army knife (and less likely to cause self-injury!), all these dazzling new features and improvements have indisputably made PHP and MySQL more difficult for beginners to learn.

(^1) http://wordpress.com/

Worse yet, PHP has completely abandoned several of the beginner-friendly features that gave it a competitive advantage in 2001, because they turned out to be oversim- plifications, or could lead inexperienced programmers into building web sites with gaping security holes. This is a problem if you’re the author of a beginner’s book about PHP and MySQL. PHP and MySQL have changed, and those changes have made writing this book a lot more difficult. But they have also made this book a lot more important. The more twisty the path, the more valuable the map, right? In this book, I’ll provide you with a hands-on look at what’s involved in building a database driven web site using PHP and MySQL. If your web host provides PHP and MySQL support, you’re in great shape. If not, I’ll show you how to install them on Windows, Mac OS X, and Linux computers, so don’t sweat it. This book is your map to the twisty path that every beginner must navigate to learn PHP and MySQL today. Grab your favorite walking stick; let’s go hiking!

Who Should Read This Book This book is aimed at intermediate and advanced web designers looking to make the leap into server-side programming. You’ll be expected to be comfortable with simple HTML, as I’ll make use of it without much in the way of explanation. No knowledge of Cascading Style Sheets (CSS) or JavaScript is assumed or required, but if you do know JavaScript, you’ll find it will make learning PHP a breeze, since these languages are quite similar. By the end of this book, you can expect to have a grasp of what’s involved in building a database driven web site. If you follow the examples, you’ll also learn the basics of PHP (a server-side scripting language that gives you easy access to a database, and a lot more) and Structured Query Language ( SQL —the standard language for interacting with relational databases) as supported by MySQL, the most popular free database engine available today. Most importantly, you’ll come away with everything you need to start on your very own database driven site!

xx