PHP, - Lagout

Transcription

PHP,MySQL ,Javascript& HTML5 ALL-IN- ONE

PHP,MySQL ,Javascript& HTML5 ALL-IN- ONEby Steve Suehring and Janet Valade

PHP, MySQL , JavaScript & HTML5 All-in-One For Dummies Published byJohn Wiley & Sons, Inc.111 River StreetHoboken, NJ 07030-5774www.wiley.comCopyright 2013 by John Wiley & Sons, Inc., Hoboken, New JerseyPublished by John Wiley & Sons, Inc., Hoboken, New JerseyPublished simultaneously in CanadaNo part of this publication may be reproduced, stored in a retrieval system or transmitted in any form orby any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior writtenpermission of the Publisher, or authorization through payment of the appropriate per-copy fee to theCopyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley& Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.Trademarks: Wiley, the Wiley logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!,The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, andrelated trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliatesin the United States and other countries, and may not be used without written permission. MySQL is aregistered trademark of MySQL AB. JavaScript is a registered trademark of Oracle America, Inc. All othertrademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with anyproduct or vendor mentioned in this book.LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NOREPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OFTHE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BECREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIESCONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THEUNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OROTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OFA COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THEAUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATIONOR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE.FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVECHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.For general information on our other products and services, please contact our Customer CareDepartment within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.For technical support, please visit www.wiley.com/techsupport.Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some materialincluded with standard print versions of this book may not be included in e-books or in print-on-demand.If this book refers to media such as a CD or DVD that is not included in the version you purchased, youmay download this material at http://booksupport.wiley.com. For more information about Wileyproducts, visit www.wiley.com.Library of Congress Control Number: 2013932114ISBN 978-1-118-21370-4 (pbk); ISBN 978-1-118-22874-6 (ebk); ISBN 978-1-118-23134-0 (ebk);ISBN 978-1-118-26617-5 (ebk)Manufactured in the United States of America10 9 8 7 6 5 4 3 2 1

About the AuthorsSteve Suehring is the author of several technology books. Steve has writtenweb applications, big and small, for a variety of organizations and in a varietyof programming languages. Steve’s expertise is in finding creative solutionsto complex problems and complex solutions to simple problems.Janet Valade is the author of PHP & MySQL For Dummies, which is in its thirdedition. She has also written PHP & MySQL Everyday Apps For Dummies andPHP & MySQL: Your visual blueprint for creating dynamic, database-driven Websites. In addition, Janet is the author of Spring into Linux and a coauthor ofMastering Visually Dreamweaver CS3 and Flash CS3 Professional.Janet has 20 years of experience in the computing field. Most recently, sheworked as a Web designer and programmer in an engineering firm for fouryears. Prior to that, Janet worked for 13 years in a university environment,where she was a systems analyst. During her tenure, she supervised theinstallation and operation of computing resources, designed and developeda data archive, supported faculty and students in their computer usage,wrote numerous technical papers, and developed and presented seminarson a variety of technology topics.

DedicationTo Bob and Mary.– Steve SuehringThis book is dedicated to everyone who finds it useful.– Janet ValadeAuthors’ AcknowledgmentsFrom Steve Suehring: For these acknowledgements, I decided to look backat the acknowledgements section that I wrote more than 10 years ago for myfirst book, MySQL Bible. I was curious who, of all of the people I thanked inthat book (and there were a lot), should be thanked in this book, 10 yearslater. The answer: All of them. They (and you, the reader) have contributedto my ability to continue to write books (and articles, and blog posts, andeverything else.) I look forward to continued success together.From Janet Valade: First, I wish to express my appreciation to the entireopen source community. Without those who give their time and talent, therewould be no cool PHP and MySQL for me to write about. Furthermore, I neverwould have learned this software without the lists where people generouslyspend their time answering foolish questions from beginners.I want to thank my mother for passing on a writing gene, along with manyother things. And my children always for everything.And, of course, I want to thank the professionals who make it all possible.Without my agent and the people at Wiley Publishing, Inc., this book wouldnot exist. Because they all do their jobs so well, I can contribute my part tothis joint project.

Publisher’s AcknowledgmentsWe’re proud of this book; please send us your comments at http://dummies.custhelp.com.For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974,outside the U.S. at 317-572-3993, or fax 317-572-4002.Some of the people who helped bring this book to market include the following:Acquisitions, Editorial, andVertical WebsitesComposition ServicesProject Editor: Heidi Unger(Previous Edition: Jean Nelson)Acquisitions Editor: Kyle LooperCopy Editor: Debbye ButlerProject Coordinator: Patrick RedmondLayout and Graphics: Jennifer CreaseyProofreaders: Jessica Kramer, Sossity R. SmithIndexer: BIM Indexing & Proofreading ServicesTechnical Editor: Peter VeverkaEditorial Manager: Kevin KirschnerVertical Websites: Richard GravesEditorial Assistant: Annie SullivanSr. Editorial Assistant: Cherie CaseCover Photo: pagadesign/iStockphotoPublishing and Editorial for Technology DummiesRichard Swadley, Vice President and Executive Group PublisherAndy Cummings, Vice President and PublisherMary Bednarek, Executive Acquisitions DirectorMary C. Corder, Editorial DirectorPublishing for Consumer DummiesKathleen Nebenhaus, Vice President and Executive PublisherComposition ServicesDebbie Stailey, Director of Composition Services

Contents at a GlanceIntroduction. 1Book I: Getting Started with PHP & MySQL. 5Chapter 1: Understanding the Languages of the Web. 7Chapter 2: Installing a Web Server. 21Chapter 3: Installing PHP. 35Chapter 4: Setting Up MySQL. 55Chapter 5: Setting Up Your Web Development Environmentwith the XAMPP Package. 75Book II: HTML and CSS. 87Chapter 1: Creating a Basic Page with HTML. 89Chapter 2: Adding Style with CSS. 121Chapter 3: Creating and Styling Web Forms. 169Book III: JavaScript. 185Chapter 1: Understanding JavaScript Basics. 187Chapter 2: Building a JavaScript Program. 191Chapter 3: Adding jQuery. 219Chapter 4: Reacting to Events with JavaScript and jQuery. 241Chapter 5: Troubleshooting JavaScript Programs. 261Book IV: PHP. 269Chapter 1: Understanding PHP Basics. 271Chapter 2: Building PHP Scripts. 319Chapter 3: PHP and Your Operating System. 365Chapter 4: Object-Oriented Programming. 397Chapter 5: Considering PHP Security. 425Chapter 6: Tracking Visitors with Sessions. 437Book V: MySQL. 447Chapter 1: Introducing MySQL. 449Chapter 2: Administering MySQL. 457Chapter 3: Designing and Building a Database. 475Chapter 4: Using the Database. 497Chapter 5: Communicating with the Database from PHP Scripts. 515

Book VI: Web Applications. 529Chapter 1: Improving Your PHP Programs. 531Chapter 2: Creating and Using a Web Service. 541Chapter 3: Validating Web Forms with JavaScript and PHP. 555Chapter 4: Building a Members-Only Website. 587Book VII: PHP and Templates. 633Chapter 1: Configuring PHP. 635Chapter 2: Building a Templating System. 641Index. 655

Table of ContentsIntroduction. 1About This Book. 1Foolish Assumptions. 1How This Book Is Organized. 2Book I: Getting Started with PHP and MySQL. 2Book II: HTML and CSS. 2Book III: JavaScript. 2Book IV:PHP. 2Book V: MySQL. 2Book VI: Web Applications. 2Book VII: PHP and Templates. 2Companion Website. 3Icons Used in This Book. 3Where to Go from Here. 3Book I: Getting Started with PHP & MySQL. 5Chapter 1: Understanding the Languages of the Web . . . . . . . . . . . . . . 7Understanding How the Web Works. 7The web browser. 8The web server. 8Understanding Web Page Languages. 10Marking up with HTML. 10Styling pages with CSS. 11Changing behaviors with JavaScript. 11Understanding the Language of Web Servers. 12Building dynamic web applications with PHP and MySQL. 12Sending the page to the browser with Apache. 13Choosing How You Want to Develop. 14Choosing a host for your website. 14Hosting for a company website. 15Choosing a web-hosting company. 16Using a hosted website. 18Setting Up Your Local Computer for Development. 19Installing the web server. 19Installing PHP. 20Installing MySQL. 20

xiiPHP, MySQL, JavaScript & HTML5 All-in-One For DummiesChapter 2: Installing a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Testing Your Web Server. 21Obtaining Apache. 22Selecting a version of Apache. 22Downloading from the Apache website. 23Obtaining Apache for Windows. 23Obtaining Apache for Linux. 23Obtaining Apache for Mac. 24Obtaining all-in-one installation kits. 24Verifying a downloaded file. 24Installing Apache. 25Installing Apache on Windows. 25Installing Apache on a Mac. 27Installing Apache from source code on Linux and Mac. 27Starting and Stopping Apache. 28Starting and stopping Apache on Windows. 28Starting Apache on Linux, Unix, and Mac. 29Restarting Apache on Linux, Unix, and Mac. 30Stopping Apache on Linux, Unix, and Mac. 30Getting Information from Apache. 31Getting Apache information on Windows. 31Getting Apache information on Linux, Unix, and Mac. 31Configuring Apache. 32Changing settings. 32Changing the location of your Document Root. 33Changing the port number. 33Chapter 3: Installing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Checking the PHP Installation. 36Obtaining PHP. 36Downloading from the PHP website. 37Obtaining PHP for Windows. 37Obtaining PHP for Linux. 37Obtaining PHP for the Mac OS. 38Obtaining all-in-one installation kits. 38Verifying a downloaded file. 39Installing PHP. 40Installing on Unix and Linux. 40Installing on Mac OS X. 42Installation options for Unix, Linux, and Mac. 44Installing on Windows. 46Configuring Your Web Server for PHP. 47Configuring your web server on Windows. 47Configuring Apache on Linux and Mac. 49Configuring PHP. 50

Table of ContentsxiiiTesting PHP. 51Troubleshooting. 53Unable to change PHP settings. 53Displays error message: Undefined function. 53Displays a blank page or HTML output only. 53Chapter 4: Setting Up MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Checking the MySQL Installation. 55Finding out if MySQL is running or installed. 56Starting MySQL. 56Obtaining MySQL. 57Downloading from the MySQL website. 58Obtaining MySQL for Windows. 58Obtaining MySQL for Linux and Unix. 58Obtaining MySQL for Mac. 59Obtaining all-in-one installation kits. 59Verifying a downloaded file. 59Installing MySQL. 59Running the MySQL Setup Wizard on Windows. 60Installing MySQL on Linux from an RPM file. 61Installing MySQL on Mac from a DMG file. 62Installing MySQL from source files. 63Configuring MySQL. 65Starting and Stopping the MySQL Server. 66Controlling the server on Windows. 66Controlling the MySQL server on Linux and Mac. 67Testing MySQL. 68Troubleshooting MySQL. 69Displays error message: Access denied. 69Displays error message: Client does not supportauthentication protocol. 69Displays error message: Can’t connect to . . . 70MySQL error log. 70The MySQL Administration Program. 70Activating MySQL Support. 71Activating MySQL support on Windows. 71Activating MySQL support on Linux and the Mac OS. 71Checking MySQL support. 72Troubleshooting PHP and MySQL. 73Displays error message: Undefined function. 73MySQL functions not activated (Windows). 74Chapter 5: Setting Up Your Web Development Environmentwith the XAMPP Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Obtaining XAMPP. 75Installing XAMPP. 76

xivPHP, MySQL, JavaScript & HTML5 All-in-One For DummiesUsing the XAMPP Control Panel. 78Testing Your Development Environment. 79Opening the XAMPP web page. 80Testing phpMyAdmin. 81Testing PHP. 81Configuring Your Development Environment. 82Configuring PHP. 83Configuring Apache. 83Configuring MySQL. 84Uninstalling and Reinstalling XAMPP. 84Troubleshooting. 85Book II: HTML and CSS. 87Chapter 1: Creating a Basic Page with HTML . . . . . . . . . . . . . . . . . . . . 89Understanding the HTML Building Blocks. 89Document types. 90Sections of an HTML Document. 91The root element. 92The head section and title element. 92The body section. 94Creating Good HTML. 94Using the appropriate elements. 94Putting text on a page. 95Creating your first page. 97Choosing block-level or inline elements. 98Inserting line breaks and spaces. 99Making your document easier to maintain. 101Adding lists and tables. 102Practicing Creating a Table. 105Including Links and Images on Your Web Page. 108Adding links. 108Adding images. 113Writing Valid HTML. 116Validating Your HTML. 117Chapter 2: Adding Style with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Discovering What CSS Can and Can’t Do for Your Web Page. 121What is CSS?. 121Why use CSS?. 122Limitations of CSS. 122Connecting CSS to a Page. 123Adding styling to an HTML element. 123Using an internal style sheet. 126Using an external style sheet. 128

Table of ContentsxvTargeting Styles. 129Selecting HTML elements. 130Selecting individual elements. 130Selecting a group of elements. 131Changing Fonts. 134Setting the font family. 134Setting font size. 136Setting the font color. 138Adding Borders. 140Changing List Styles. 144Changing bullet styles. 145Removing bullets. 146Adding a Background. 147Changing the background color. 147Adding a background image. 150Creating Page Layouts. 155Creating a single-column layout. 155Creating a two-column layout. 159Adding Headers and Footers to a Page. 163Creating a header, header menu, and footer. 163Examining the HTML and CSS files. 166Chapter 3: Creating and Styling Web Forms . . . . . . . . . . . . . . . . . . . . . 169Using Web Forms to Get Information.

Janet Valade is the author of PHP & MySQL For Dummies, which is in its third edition. She has also written PHP & MySQL Everyday Apps For Dummies and PHP & MySQL: Your visual blueprint for creating dynamic, database-driven Web sites. In addition, J