Learning PHP, MySQL, And JavaScript

3y ago
56 Views
7 Downloads
7.26 MB
530 Pages
Last View : 1d ago
Last Download : 5m ago
Upload by : Lilly Kaiser
Transcription

Learning PHP, MySQL, and JavaScript

Learning PHP, MySQL, andJavaScriptRobin NixonBeijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Learning PHP, MySQL, and JavaScriptby Robin NixonCopyright 2009 Robin Nixon. All rights reserved.Printed in the United States of America.Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.O’Reilly books may be purchased for educational, business, or sales promotional use. Online editionsare also available for most titles (http://my.safaribooksonline.com). For more information, contact ourcorporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.Editor: Andy OramProduction Editor: Sumita MukherjiCopyeditor: Nancy KotaryProofreader: Kiel Van HornIndexer: Ellen Troutman ZaigCover Designer: Karen MontgomeryInterior Designer: David FutatoIllustrator: Robert RomanoPrinting History:July 2009:First Edition.Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks ofO’Reilly Media, Inc. Learning PHP, MySQL, and JavaScript, the image of sugar gliders, and related tradedress are trademarks of O’Reilly Media, Inc.Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of atrademark claim, the designations have been printed in caps or initial caps.While every precaution has been taken in the preparation of this book, the publisher and author assumeno responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.TMThis book uses RepKover , a durable and flexible lay-flat binding.ISBN: 978-0-596-15713-5[M]1265393701[2/10]

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv1. Introduction to Dynamic Web Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1HTTP and HTML: Berners-Lee’s BasicsThe Request/Response ProcedureThe Benefits of PHP, MySQL, and JavaScriptUsing PHPUsing MySQLUsing JavaScriptThe Apache Web ServerAbout Open SourceBringing It All TogetherTest Your Knowledge: Questions225567899112. Setting Up a Development Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13What Is a WAMP, MAMP, or LAMP?Installing a WAMP on WindowsOvercoming Installation ProblemsTesting the InstallationAlternative WAMPsInstalling a MAMP on Mac OS XSome Final TweakingOther AlternativesInstalling a LAMP on LinuxWorking RemotelyLogging InUsing FTPUsing a Program EditorUsing an IDETest Your Knowledge: Questions131414161819242525262727283032v

3. Introduction to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Incorporating PHP Within HTMLCalling the PHP ParserThis Book’s ExamplesThe Structure of PHPUsing CommentsBasic SyntaxUnderstanding VariablesOperatorsVariable AssignmentMultiple-Line CommandsVariable TypingConstantsThe Difference Between the echo and print CommandsFunctionsVariable ScopeTest Your Knowledge: Questions333435363637384245474950515253584. Expressions and Control Flow in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61ExpressionsLiterals and VariablesOperatorsOperator PrecedenceAssociativityRelational OperatorsConditionalsThe if StatementThe else StatementThe elseif StatementThe switch StatementThe ? OperatorLoopingwhile Loopsdo.while Loopsfor LoopsBreaking Out of a LoopThe continue StatementImplicit and Explicit CastingPHP Dynamic LinkingDynamic Linking in ActionTest Your Knowledge: Questionsvi Table of 87

5. PHP Functions and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89PHP FunctionsDefining a FunctionReturning a ValueReturning an ArrayPassing by ReferenceReturning Global VariablesRecap of Variable ScopeIncluding and Requiring FilesThe include StatementUsing include onceUsing require and require oncePHP Version CompatibilityPHP ObjectsTerminologyDeclaring a ClassCreating an ObjectAccessing ObjectsConstructorsWriting MethodsDeclaring PropertiesDeclaring ConstantsProperty and Method Scope in PHP 5InheritanceTest Your Knowledge: 051061071071091136. PHP Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Basic AccessNumerically Indexed ArraysAssociative ArraysAssignment Using the array KeywordThe foreach.as LoopMultidimensional ArraysUsing Array Functionsis 4125125126127128Table of Contents vii

Test Your Knowledge: Questions1287. Practical PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Using printfPrecision SettingString PaddingUsing sprintfDate and Time FunctionsDate ConstantsUsing checkdateFile HandlingChecking Whether a File ExistsCreating a FileReading from FilesCopying FilesMoving a FileDeleting a FileUpdating FilesLocking Files for Multiple AccessesReading an Entire FileUploading FilesSystem CallsXHTMLThe Benefits of XHTMLXHTML VersionsWhat’s Different?HTML 4.01 Document TypesXHTML 1.0 Document TypesXHTML ValidationTest Your Knowledge: 01411421431441491511511511521531531541558. Introduction to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157MySQL BasicsSummary of Database TermsAccessing MySQL via the Command LineStarting the Command-Line InterfaceUsing the Command-Line InterfaceMySQL CommandsData TypesIndexesCreating an IndexQuerying a MySQL DatabaseJoining Tables Togetherviii Table of Contents157158158159163164168177178183192

Using Logical OperatorsMySQL FunctionsAccessing MySQL via phpMyAdminWindows UsersMac OS X UsersLinux UsersUsing phpMyAdminTest Your Knowledge: Questions1941941951951951951971989. Mastering MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Database DesignPrimary Keys: The Keys to Relational DatabasesNormalizationFirst Normal FormSecond Normal FormThird Normal FormWhen Not to Use y-to-ManyDatabases and AnonymityTransactionsTransaction Storage EnginesUsing BEGINUsing COMMITUsing ROLLBACKUsing EXPLAINBacking Up and RestoringUsing mysqldumpCreating a Backup FileRestoring from a Backup FileDumping Data in CSV FormatPlanning Your BackupsTest Your Knowledge: 521621621621721821922022222222322310. Accessing MySQL Using PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225Querying a MySQL Database with PHPThe ProcessCreating a Login FileConnecting to MySQLA Practical ExampleThe POST Array225225226227232234Table of Contents ix

Deleting a RecordDisplaying the FormQuerying the DatabaseRunning the ProgramPractical MySQLCreating a TableDescribing a TableDropping a TableAdding DataRetrieving DataUpdating DataDeleting DataUsing AUTO INCREMENTPerforming Additional QueriesPreventing SQL InjectionPreventing HTML InjectionTest Your Knowledge: 424524825011. Form Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251Building FormsRetrieving Submitted Dataregister globals: An Old Solution Hangs OnDefault ValuesInput TypesText BoxesText AreasCheckboxesRadio ButtonsHidden FieldsSelectLabelsSanitizing InputAn Example ProgramTest Your Knowledge: 426612. Templating with Smarty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Why Smarty?InstallationCreating ScriptsCreating TemplatesA Practical ExampleTest Your Knowledge: Questionsx Table of Contents270270271272272277

13. Cookies, Sessions, and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Using Cookies in PHPSetting a CookieAccessing a CookieDestroying a CookieHTTP AuthenticationStoring Usernames and PasswordsSaltingUsing SessionsStarting a SessionEnding a SessionSession SecurityTest Your Knowledge: Questions27928128128228228528528928929229329614. Exploring JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299JavaScript and HTML TextUsing Scripts Within a Document HeadOlder and Nonstandard BrowsersIncluding JavaScript FilesDebugging JavaScript ErrorsUsing CommentsSemicolonsVariablesString VariablesNumeric VariablesArraysOperatorsArithmetic OperatorsAssignment OperatorsComparison OperatorsLogical OperatorsVariable Incrementing and DecrementingString ConcatenationEscaping CharactersVariable TypingFunctionsGlobal VariablesLocal VariablesThe Document Object ModelBrowser IncompatibilitiesUsing the DOMTest Your Knowledge: 8309309310310310311312312312314316317318Table of Contents xi

15. Expressions and Control Flow in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319ExpressionsLiterals and VariablesOperatorsOperator PrecedenceAssociativityRelational OperatorsThe with StatementUsing onErrorUsing try.catchConditionalsThe if StatementThe switch StatementThe ? OperatorLoopingwhile Loopsdo.while Loopsfor LoopsBreaking Out of a LoopThe continue StatementExplicit CastingTest Your Knowledge: 133133233233333433433516. JavaScript Functions, Objects, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337JavaScript FunctionsDefining a FunctionReturning a ValueReturning an ArrayJavaScript ObjectsDeclaring a ClassCreating an ObjectAccessing ObjectsThe prototype KeywordJavaScript ArraysNumeric ArraysAssociative ArraysMultidimensional ArraysUsing Array MethodsTest Your Knowledge: 935317. JavaScript and PHP Validation and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . 355Validating User Input with JavaScriptThe validate.html Document (Part One)xii Table of Contents355356

The validate.html Document (Part Two)Regular ExpressionsMatching Through MetacharactersFuzzy Character MatchingGrouping Through ParenthesesCharacter ClassesIndicating a RangeNegationSome More Complicated ExamplesSummary of MetacharactersGeneral ModifiersUsing Regular Expressions in JavaScriptUsing Regular Expressions in PHPRedisplaying a Form After PHP ValidationTest Your Knowledge: 037518. Using Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377What Is Ajax?Using XMLHttpRequestYour First Ajax ProgramUsing GET Instead of POSTSending XML RequestsTest Your Knowledge: Questions37837838038538739119. Using YUI for Ajax and More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Choosing a FrameworkUsing YUICompressed VersionsUsing YUI for AjaxOther Uses for YUIA Simple YUI CalendarTest Your Knowledge: Questions39339439639640040140320. Bringing It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405Designing a Social Networking SiteAbout Third-Party Add-OnsOn the Websiternfunctions.phpThe php405406406406407409410411412Table of Contents xiii

Checking for Username Availabilityrnsignup.php (YUI ing the “About Me” TextAdding a Profile ImageProcessing the ImageDisplaying the Current Profilernmembers.phpViewing a User’s ProfileAdding and Dropping FriendsListing All 15417417419420420420421424424424424427430432A. Solutions to the Chapter Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435B. Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453C. MySQL’s FULLTEXT Stopwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457D. MySQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461E. Using PEAR and PHPUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485xiv Table of Contents

PrefaceThe combination of PHP and MySQL is the most convenient approach to dynamic,database-driven web design, holding its own in the face of challenges from integratedframeworks—such as Ruby on Rails—that are harder to learn. Due to its open sourceroots (unlike the competing Microsoft .NET framework), it is free to implement and istherefore an extremely popular option for web development.Any would-be developer on a Unix/Linux or even a Windows/Apache platform willneed to master these technologies. At the same time, the JavaScript is important, as itprovides the hidden communication with the web server to create seamless interfaces.AudienceThis book is for people who wish to learn how to create effective and dynamic websites.This may include webmasters or graphic designers who are already creating static websites but wish to take their skills to the next level as well as high scho

The combination of PHP and MySQL is the most convenient approach to dynamic, database-driven web design, holding its own in the face of challenges from integrated frameworks—such as Ruby on Rails—that are harder to learn. Due to its open source roots (unlike the competing Microsoft .NET framework), it is free to implement and is therefore an extremely popular option for web development .

Related Documents:

MySQL PHP Syntax MySQL works very well in combination of various programming languages like PERL, C, C , JAVA and PHP. Out of these languages, PHP is the most popular one because of its web application development capabilities. PHP provides various functions to access MySQL database and to manipulate data records inside MySQL database.

Section 4: PHP and MySQL - The Structured Repository 4.1 PHP MySQL Connectivity 4.2 Integrating Web Forms and Database 4.3 Using PHP’s MySQL Extension 4.4 Using PHP’s PDO Extension Section 5: Learn More Advanced Techniques in PHP 5.1 Introduction to Object Oriented Programming 5.2 Classes and Objects

MySQL is no longer enabled by default, so the php_mysql.dllDLL must be enabled inside of php.ini. Also, PHP needs access to the MySQL client library. A file named libmysql.dllis included in the Windows PHP distribution and in order for PHP to talk to MySQL this file needs to be available to the Windows systems PATH. See the FAQ titled "How do I add my PHP directory to the PATHon Windows" for .

PHP is FREE to download from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side Where to Start? To get access to a web server with PHP support, you can: Install Apache (or IIS) on your own server, install PHP, and MySQL Or find a web hosting plan with PHP and

PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use What is a PHP File? PHP files can contain text, HTML, CSS, JavaScript, and PHP code PHP code are executed on the server, and the result is returned to the browser .

MySQL for Excel is a 32-bit add-in for Microsoft Excel, which you can install and run on Microsoft Windows. MySQL for Excel is not compatible with Linux or macOS. MySQL for Excel can interact with MySQL Workbench to simplify the management of MySQL connections when both MySQL client tools are installed.

Lifetime Support Oracle Premier Support Oracle Product Certifications MySQL Enterprise High Availability MySQL Enterprise Security MySQL Enterprise Scalability MySQL Enterprise Backup MySQL Enterprise Monitor/Query Analyzer MySQL Workbench MySQL Enterprise Edition. 11 MySQL Database

uqc103s/UFCE47-20-1 PHP-mySQL 7 Why PHP and mySQL „ MySQL is a key part of LAMP (Linux, Apache, MySQL, PHP / Perl / Python), a fast growing open source enterprise software stack. More and more companies are using LAMP as an alternative to expensive proprietary software stacks because of its lower cost and freedom from lock-in.