1. Trang chủ
  2. » Giáo Dục - Đào Tạo

PHP 5 MySQL programming for the absolute beginner tủ tài liệu bách khoa

465 67 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

Thông tin cơ bản

Định dạng
Số trang 465
Dung lượng 15,07 MB

Nội dung

www.allitebooks.com www.allitebooks.com This page intentionally left blank www.allitebooks.com PHP 5/MySQL Programming ANDY HARRIS www.allitebooks.com © 2004 by Thomson Course Technology PTR All rights reserved No part of this book 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 written permission from Thomson Course Technology PTR, except for the inclusion of brief quotations in a review The Thomson Course Technology PTR logo and related trade dress are trademarks of Thomson Course Technology PTR and may not be used without written permission Microsoft, Windows, Internet Explorer, Notepad, VBScript, ActiveX, and FrontPage are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Netscape is a registered trademark of Netscape Communications Corporation in the U.S and other countries PHP is copyright © 2001-2004 The PHP Group MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries All other trademarks are the property of their respective owners Important: Thomson Course Technology PTR cannot provide software support Please contact the appropriate software manufacturer’s technical support line or Web site for assistance Thomson Course Technology PTR and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer Information contained in this book has been obtained by Thomson Course Technology PTR from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, Thomson Course Technology PTR, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information Readers should be particularly aware of the fact that the Internet is an ever-changing entity Some facts may have changed since this book went to press SVP, Thomson Course Technology PTR: Andy Shafran Publisher: Stacy L Hiquet Senior Marketing Manager: Sarah O’Donnell Marketiing Manager: Heather Hurley Manager of Editorial Services: Heather Talbot Acquisitions Editor: Mitzi Koontz Senior Editor: Mark Garvey Associate Marketting Managers: Kristin Eisenzopf and Sarah Dubois Project Editor: Scott Harris/Argosy Publishing Technical Reviewer: Arlie Hartman Thomson Course Technology PTR Market Coordina ator: Amanda Weaver Copy Editor: Educational facilities, companies, and organizations interested in multiple copies or licensing of this book should contact the publisher for quantity discount information Training manuals, CD-ROMs, and portions of this book are also available individually or can be tailored for specific needs Tonya Cupp ISBN: 1-59200-494-6 Library of Congress Catalog Card Number: 2004108011 Printed in the United States of America 04 05 06 07 08 BH 10 Mike Tanamachi Interior Layout Tech: Shawn Morningstar Cover Designer: CD-ROM Producer: Arlie Hartman Indexer: Maureen Shepherd Proofreadeer: Thomson Course Technology PTR, a division of Thomson Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com www.allitebooks.com Jan Cocker To Heather, Elizabeth, Matthew, and Jacob, and to all those who have called me Teacher www.allitebooks.com Acknowledgments F irst I thank Him from whom all flows Heather, you always work harder on these books than I Thank you for your love and your support Thank you Elizabeth, Matthew, and Jacob for understanding why Daddy was typing all the time Thanks to the Open Source community for creating great free software like PHP and MySQL Also, thanks to the phpMyAdmin team and the SQLite team for developing such terrific software and making it freely available Thank you, Stacy Hiquet, for your continued support and encouragement on this and other projects Thanks, Scott Harris You did a great job of juggling all those balls around Thanks to Tonya Cupp for drastically improving the readability of the manuscript Arlie Hartman, thank you for technical editing and for putting together the CD-ROM Thanks to J Wynia (www.phpgeek.com) for technical editing Thanks also to Jason for use of PHPTriad on the CD-ROM Special thanks to those who worked on the first edition Your hard work is the foundation for something even better Thank you to the many members of the Premier/Course team who worked on this book A huge thanks goes to my CSCI N399 and N452 Server-Side Web Development classes and the many people who sent in comments and advice from the first edition Thank you for being patient with my manuscript, for helping me spot many errors, and for providing invaluable advice I learned as much from you as you did from me www.allitebooks.com About the Author A ndy Harris began his teaching career as a high-school special education teacher During that time, he taught himself enough computing to part-time computer consulting and database work He began teaching computing at the university level in the late 1980s as a part-time job Since 1995 he has been a full-time lecturer in the Computer Science Department of Indiana University/Purdue University–Indianapolis, where he manages the Streaming Media Lab and teaches classes in several programming languages His primary interests are PHP, Java, Microsoft languages, Perl, JavaScript, Web Data, virtual reality, portable devices, and streaming media He has written numerous books on these and other technology topics www.allitebooks.com This page intentionally left blank www.allitebooks.com Contents at a Glance Introduction xxi Chapter 1: Exploring the PHP Environment Chapter 2: Using Variables and Input 21 Chapter 3: Controlling Your Code with Conditions and Functions 55 Chapter 4: Loops and Arrays 95 Chapter 5: Better Arrays and String Handling Chapter 6: Working with Files 133 181 Chapter 7: Writing Programs with Objects 229 Chapter 8: XML and Content Management Systems 271 Chapter 9: Using MySQL to Create Databases 299 Chapter 10: Connecting to Databases within PHP 335 Chapter 11: Data Normalization 359 Chapter 12: Building a Three-Tiered Data Application 383 Appendix A: Reviewing HTML and Cascading Style Sheets on cd Appendix B: Using SQLite as an Alternative Data Source on cd Index 429 www.allitebooks.com PHP 5/MySQL Programming for the Absolute Beginner 428 CHALLENGES Add a module that lets the user interactively query the database Begin with a page that allows the user to type in an agent’s name and returns data based on that agent Once the basic functionality of an agent search program is done, add checkboxes that allow certain agent aspects (operation and skills) to be displayed Build programs that allow searching on other aspects of the data, including skills and operations Modify the SpyMaster database to support another data set or SQLite Index Symbols * asterisk, 33, 420 {} braces, 62, 66, 71, 79, 100, 198 | pipeline, 198, 199 / forward slash, 33, 420 \ backward slash, 98, 198 : line separation character,, 182 ; line termination character, 27, 62 ; beginning extension character, 17 ; if statement character, 62, 63 ; insiide string character, 30 $ end string indicator, 199, 420 $i sentry variable, 99, 100, 103, 137 $ variable naming character, 106, 198, 199 + increment operator, 33, 198 ++ increment operator, 100 – minus sign, 33 = assignment operator, 26, 32 != = comparison operator, 197 = = comparison operator, 62 > = comparison operator, 62 < = comparison operator, 62 ! = comparison n operator, 62 concatenation operator, 159 regular expression operator, 198 –> arrow syntax, 233, 234 < ? ?> scriipt tag, 12–13, 98, 238, 265 ^ regular expression operator, 198 A access methods, 234, 252, 256 access modifiers, 188 Ace or Not program, 64–66 action attributes, 35 addFoils() function, 163, 175–176 adding PHP commands, 12–15 add record module, 391, 422–425 addText() function, 235–237, 261–262 addWord() function, 168–174 ad hoc list, 239 admiinistrative password, 213–214 Adventure Generator program committing changes, 355–356 connections, 342–343 data structure, 328–333 editing records, 350–355 overview, 300–302 segment display, 343–348 viewing records, 348–350 AND structure, 375–376 answer key creating, 223 passing, 178 puzzle, 177–179 Apache configuring, overview, 5, 6–7 running, 9–10 starting as service, 8–9 testing server, 7–8 append access, 224–225 application programming interface (API), 289–291, 293–296 array() function, 110, 139–140 arrays, 95 See also loops; variables associative, 137 array() function, 139–140 building, 137–140 built-in, 141–144 foreach loops, 140–141 forms, 141–144 two-dimensional, 150–154 combining with loops, 109–113 generating, 109 multidimensional See also databases; tables building, 144–147 distance query responses, 147–149 preloading, 110 size detection, 110–111 structure, 107–108 foreach loops, 135–137 list creation, 264–265 reading files Cartoonifier program, 192–193 loading file, 193–194 modifying file, 194 splitting line into, 203 table creation, 265–266 two-dimensional, 241 Index 430 arrow syntax (–>), 233, 234 AS clause, 373 asking questions, 33–36 assignment operator (=), 26, 32 associative arrays array() function, 139–140 building, 137–140 built-in $_request array, 141–144 formReader program, 141 foreach loops, 140–141 forms debugging, 144 reading, 141–144 two-dimensional building, 150–151, 153 getting data, 154 queries, 151–153 assterisk ( * ), 33, 420 AUTO_INCREMENT identifier, 370 B background style, 282–283 Bad While program, 105–106 blocks page, 277–278 Boolean variables, 167 Border Maker program building, 41–43 form elements, 43–45 overview, 40–41 radio groups, 46 select elements, 45 botttom of Web page, 263 braces ({}) function definition, 79 if statements, 62, 66 for loops, 100 regular expression operators, 198 switch structure, 71 break statement, 71 browsers, 283 buildBottom() function, 232–233, 263 buildButton() function, 347–348 buildHTML() function, 216–220 buildList() function, 238–239, 264–265 buildTable() function, 239–241, 245–247, 265–266 buildTop() function n, 232–233, 262 business rules, 360 C calcNumPetals function, 92–93 carriage return, 98 Cartoonifier program, 192–193 cascading style sheets (CSSs) associate arrays, 139 attributes, 40–41 database connection, 345 library module, 406–407 text appearance, 191 viewing, 281–283 ca ase sensitivity, 25, 286 case statement, 71 character translation, 159–160 checkboxes, 74–77, 122 chorus() function, 83–84 city namees, 148 class keywords, 251, 252 clearBoard() function, 166 client-side programs, closing files, 189 code body encapsulation, 82–83 Poker Dice program, 118–119 colo on (:), 182 columns, database, 305 324 comma-delimited format, 201 comma-separated value (CSV), 201, 319 comment characters, 314 comparison operators, 62, 197 concatenation operator, 159 conditions Ace program, 60–61 adding, 325–326 spy database, 375–376 multiple, 326–327 configuring PHP register globals, 15 safe mode, 15 Windows extensions, 16–18 connectToSpy() function, 397, 408 constructors, 254–255, 261 content management system (CMS) features, 273–274 overview, 3, 271–273 PHP-Nuke customizing, 277–278 installing, 276 working with, 274–276 simple examining code, 280–281 menu system, 283–285 viewing CSS, 281–283 viewing pages, 279–280 431 D database environment requirements, SpyMaster, 394–395 database management system See relational database management system (RDBMS) data definition, 340, 391 data extraction, 297–298 data normalization concept, 363–364 first normal form, 364 second normal form, 365 third normal form, 365–366 debugging forms, 144 default case, switch sttructure, 71 delete record module, 392, 404, 422 DELETE statement, 422 DESCRIBE statement, 310 description field, 346 develop pment environment, 5–6 dice games Ace, 59–61 Ace or Not, 64–66 Binary Dice, 66–68 Petals, 88–94 Poker Dice, 96, 114 Roll Em, 57–58 Switch Dice, 69–71 directory handles, 196 directory information directory handles, 196 file lists, 196–197 file selection, 197 imageIndex program, 194–196 output storage, 199–200 regular expressions, 197–199 distance queries, 147–149 divvision symbol (/), 32 div tag, 281 dollar signs, 87 drop-down menus library module, 426 phpMyAdmin, 317 SuperHTML, 244–245 E East code, 171–173 editors, editQuiz program, 210, 212–215 edit record module, 391, 402 generic form, 410–413 smarter form, 415–418 editSegment program, 350–354 edit table modu ule, 391, 397–399, 401, 410–413 else clause Ace or Not program, 65–66 multiple, 68 embedded form data, 38–40 empty function, 77 encapsulation main code body, 82–83 object-oriented programming (OOP), 249 This Old Man program, 77–80 enclosure, lists, 238, 265 ending lines character (;), 27, 62 ending strings character ($), 199, 420 endless loops, 105–106 endTable() function, 241, 266 entity-rellationship (ER) diagram, 366, 376–377 equals sign (=), 26, 32 error messages, 27 evaluate() function, 123–129 events, object, 249 exiting loops, 99, 107 eXtensible Markup Language (XML), 271 data extraction, 297–298 introduction, 285–287 main page, 287 menu pages, 288 more-complex model, 293–296 parsers, 288–289 simple model, 289–293 table creation, 320–321 extensions PHP, 304 Windows, 16–18 extraction, data, 297–298 Index XML, 285–286 data extraction, 297–298 main page, 287 menu pages, 288 more-complex, 293–296 parsers, 288–289 rules, 286–287 simple, 289 simple API, 289–293 working with, 286 control panel, Apache, 8–9 count() function backwards, 102–103 by fives, 100–102 form fields, 114–116 overview, 110–111 Poker Dice program, 126–129 critter class, 250–252, 257–260 432 Index F Fancy Old Man program, 111–113 fclose() function, 189 feof() function, 189, 191–192 fgets() function, 189, 192, 194, 203 field queries, 40 fieelds, database, 305, 308, 309 getting names, 340–341 library module, 418–419 listed, 362–363 naming, 373 fieldToList() function, 426 file() function, 193–194, 202–203 file handles (pointers), 187 filename, 187 fiile ownership, 187 file pointers See file handles file systems closing file, 189 file access modifiers, 188 file handle, 187 opening file, 187 saveSonnet program, 185–186 writing to file, 189 fillBoard() function, 166–168 finishing loops, 99, 107 first normal form, 364 five of a kind, 128–129 foil letters, 175–176 fopen() function, 187, 189 foreach loops associative arrays, 140–141 database storage, 342 loading files, 193 modifying files, 194 splitting function, 203 storing output, 199–200 versus for loops, 137 with arrays, 135–137 XML, 292–293 foreign key, 371, 377, 418, 419–420 foreign key list box, 420 for loops counting with, 96–98 building loop, 100 finishing conditions, 99 sentry variable changes, 99–100 sentry variable initialization, 98–99 examining array’s contents, 109–111 modifying counting backwards, 102–103 counting by fives, 100–102 versus foreach loops, 137 Word Search program, 166 form fields counting with, 114–116 hidden, 117 form input response, 268 form objects, 267 formResults() function, 247–248, 268 forms borderMaker program, 40–46 building HTML page, 34–35 checking, story program, 51–53 combining with results, 71–73 creating, 33–34 library module generic, 413–415 smarter edit, 415–418 setting action attribute, 35 superHTML, 241–248 drop-down menus, 244–245 tables, 245–247 text boxes, 243–244 viewing results, 247–248 writing data-retrieval script, 35–36 forward slash (//), 33, 420 four of a kind, 128–129 fourth-generation languages, 307 fputs() function, 189 frames, HTML, 280 free PHP hostin ng, FROM clause, 374 full house, 128 functional dependency, 365–366 G gAddText() function, 262 get field names, 340–341 getFiles() function, 205–206 get method, 36–38, 284 getName() function, 256 getPage() function, 233, 261 getter methods, 256 get variant, buildTable() function, 245 global keywords, 407 global varia ables, 86 gradeQuiz program, 222–223, 225 graphics, dice games, 58–59 greater than symbol (>), 62 gTextBox() function, 267 433 J headers, 264 HERE token, 80 hidden form fields, 45, 117 Hi Jacob program, 23–25 Hi User program, 71–73 htdocs directory, 8, 276 httdocs subdirectory, 9–10 HTML (hypertext markup language) associative arrays, 150–151 forms, 33–36 library module, 409–413 multidimensional arrays, 146–147 Petals program, 88–89, 94 Poker Dice program, 117–118 story program, 48–51 quiz program, 216–220 HTTP (hypertext transfer protocol), 114 joins conditions, 375–376 creating, 373–374 using, 373 WHERE clause, 374–375 I if else clauses, 68 if statements Ace program, 59–61 combining form and results, 71–73 comparison operators, 62 creating, 62–63 multiple-condition, 68 imageIndex program, 194–196 images, printing, 58–59 include command, 231, 281, 396–397 inconsistent data, 361–362 indenting lines, 80 index.html page, inheritance, 249, 257–260 inner joins, 376 inserting table values, 311–312 installation Apache, 6–10 development environment, 5–6 MySQL, 303–304 PHP, 10–11 PHP-Nuke, 276 using existing server, instructions (syntax), 27 integers, 32 Internet Information Server (IIS), interpola ation speed, 159 troubleshooting, 175 variable, 58 interpreters, 114 item class, 283 L LAMP (Linux, Apache, MySQL, and PHP), less than symbol (

Ngày đăng: 08/11/2019, 10:59