Yi A R PHP, MySQL, JavaScript & CSS

3y ago
53 Views
2 Downloads
6.68 MB
31 Pages
Last View : 3d ago
Last Download : 3m ago
Upload by : Milo Davies
Transcription

*A Step-by-Step Guide to CreatingDynamic WebsitesPr1/ .LearningYiWs--'ÿ‘5MMm a wrPHP, MySQL,JavaScript& CSS63533m&A*B mmHIn&as»i*«*« IMsiri1X. '-l«9tvjHISii* B &Hi.1'O’REILLY Robin NixonHS5S

Free Sampler

O’Reilly Ebooks—Your bookshelf on your devices!fort*§wLPDFePubE-ED IMobiAPKDAISYWhen you buy an ebook through oreilly.com you get lifetime access to the book, andwhenever possible we provide it to you in five, DRM-free file formats—PDF, .epub,Kindle-compatible .mobi, Android .apk, and DAISY—that you can use on the devices ofyour choice. Our ebook files are fully searchable, and you can cut-and-paste and printthem. We also alert you when we’ve updated the files with corrections and additions.Learn more at ebooks.oreilly.comYou can also purchase O’Reilly ebooks through theiBookstore, the Android Marketplace, and Amazon.com.O’REILLYiSpreading the knowledge of innovatorsoreilly.com

SECOND EDITIONLearning PHP, MySQL, JavaScript,and CSSRobin NixonBeijing Cambridge Farnham Köln Sebastopol Tokyo

Learning PHP, MySQL, JavaScript, and CSS, Second Editionby Robin NixonCopyright 2012 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: Iris FebresCopyeditor: Rachel HeadProofreader: Kiel Van HornAugust 2012:Indexer: Ellen Troutman ZaigCover Designer: Karen MontgomeryInterior Designer: David FutatoIllustrator: Robert RomanoSecond Edition.Revision History for the Second Edition:2012-08-10First releaseSee http://oreilly.com/catalog/errata.csp?isbn 9781449319267 for release details.Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks ofO’Reilly Media, Inc. Learning PHP, MySQL, JavaScript, and CSS, the image of sugar gliders, and relatedtrade dress 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 authors assumeno responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.ISBN: 978-1-449-31926-7[LSI]1344626556

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii1. Introduction to Dynamic Web Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1HTTP and HTML: Berners-Lee’s BasicsThe Request/Response ProcedureThe Benefits of PHP, MySQL, JavaScript, and CSSUsing PHPUsing MySQLUsing JavaScriptUsing CSSThe Apache Web ServerAbout Open SourceBringing It All TogetherTest Your Knowledge225567991010122. Setting Up a Development Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13What Is a WAMP, MAMP, or LAMP?Installing a WAMP on WindowsTesting the InstallationAlternative WAMPsInstalling a MAMP on OS XConfiguring MySQLTesting the InstallationInstalling a LAMP on LinuxWorking RemotelyLogging InUsing FTPUsing a Program EditorUsing an IDETest Your Knowledge1314222424252831313232333436v

3. Introduction to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Incorporating 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 Knowledge373839404041424649515454565657624. Expressions and Control Flow in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65ExpressionsLiterals 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 Knowledgevi Table of 91

5. PHP Functions and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93PHP 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 51051081091101111121141176. PHP Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Basic AccessNumerically Indexed ArraysAssociative ArraysAssignment Using the array KeywordThe foreach.as LoopMultidimensional ArraysUsing Array Functionsis ble of Contents vii

Test Your Knowledge1327. Practical PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133Using 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 TypesThe HTML5 Document TypeXHTML 1.0 Document TypesXHTML ValidationTest Your 41451461471481531541551551551561571571581598. Introduction to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161MySQL BasicsSummary of Database TermsAccessing MySQL via the Command LineStarting the Command-Line InterfaceUsing the Command-Line InterfaceMySQL CommandsData TypesIndexesCreating an IndexQuerying a MySQL Databaseviii Table of Contents161162162163166168172181182187

Joining Tables TogetherUsing Logical OperatorsMySQL FunctionsAccessing MySQL via phpMyAdminWindows UsersOS X UsersLinux UsersUsing phpMyAdminTest Your Knowledge1961981991991992012012012029. Mastering MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Database 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 922022022122122322322422622622722710. Accessing MySQL Using PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229Querying a MySQL Database with PHPThe ProcessCreating a Login FileConnecting to MySQLA Practical Example229229230231236Table of Contents ix

The POST ArrayDeleting 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 724824925225411. Form Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255Building FormsRetrieving Submitted Dataregister globals: An Old Solution Hangs OnDefault ValuesInput TypesSanitizing InputAn Example ProgramTest Your Knowledge25525625825825926626727012. Cookies, Sessions, and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271Using Cookies in PHPSetting a CookieAccessing a CookieDestroying a CookieHTTP AuthenticationStoring Usernames and PasswordsSaltingUsing SessionsStarting a SessionEnding a SessionSession SecurityTest Your Knowledgex Table of Contents271273273274274277277281281284286289

13. Exploring JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291JavaScript 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 Model (DOM)But It’s Not That SimpleUsing the DOMTest Your 030130130130230230330330430430530730830914. Expressions and Control Flow in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311ExpressionsLiterals and VariablesOperatorsOperator PrecedenceAssociativityRelational OperatorsThe with StatementUsing onerrorUsing try.catchConditionalsThe if StatementThe switch statementThe ? le of Contents xi

Loopingwhile Loopsdo while Loopsfor LoopsBreaking Out of a LoopThe continue StatementExplicit CastingTest Your Knowledge32332332432432532632732715. JavaScript Functions, Objects, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329JavaScript FunctionsDefining a FunctionReturning a ValueReturning an ArrayJavaScript ObjectsDeclaring a ClassCreating an ObjectAccessing ObjectsThe prototype KeywordJavaScript ArraysNumeric ArraysAssociative ArraysMultidimensional ArraysUsing Array MethodsTest Your 134516. JavaScript and PHP Validation and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . 347Validating User Input with JavaScriptThe validate.html Document (Part One)The validate.html Document (Part Two)Regular ExpressionsMatching Through MetacharactersFuzzy Character MatchingGrouping Through ParenthesesCharacter ClassesSome More Complicated ExamplesSummary of MetacharactersGeneral ModifiersUsing Regular Expressions in JavaScriptUsing Regular Expressions in PHPRedisplaying a Form After PHP ValidationTest Your Knowledgexii Table of 367

17. Using Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369What Is Ajax?Using XMLHttpRequestImplementing Ajax via POST RequestsThe readyState PropertyThe Server Half of the Ajax ProcessUsing GET Instead of POSTSending XML RequestsAbout XMLWhy Use XML?Using Frameworks for AjaxTest Your Knowledge36937037237437537737938138338338318. Introduction to CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385Importing a Style SheetImporting a Style Sheet from Within HTMLEmbedded Style SettingsUsing IDsUsing ClassesCSS RulesUsing SemicolonsMultiple AssignmentsUsing CommentsStyle TypesDefault StylesUser StylesExternal Style SheetsInternal StylesInline StylesCSS SelectorsThe Type SelectorThe Descendant SelectorThe Child SelectorThe Adjacent Sibling SelectorThe ID SelectorThe Class SelectorThe Attribute SelectorThe Universal SelectorSelecting by GroupThe CSS CascadeStyle Sheet CreatorsStyle Sheet MethodsStyle Sheet e of Contents xiii

The Difference Between div and span MeasurementsFonts and tManaging Text ntingCSS ColorsShort Color StringsGradientsPositioning ElementsAbsolute PositioningRelative PositioningFixed PositioningComparing Positioning TypesPseudoclassesPseudoelementsShorthand RulesThe Box Model and LayoutSetting MarginsApplying BordersAdjusting PaddingObject ContentsTest Your 9. Advanced CSS with CSS3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423Attribute SelectorsMatching Parts of StringsThe box-sizing PropertyCSS3 BackgroundsThe background-clip PropertyThe background-origin PropertyThe background-size PropertyMultiple BackgroundsCSS3 BordersThe border-color PropertyThe border-radius Propertyxiv Table of Contents423424425425426426428428430430431

Box ShadowsElement OverflowMulticolumn LayoutColors and OpacityHSL ColorsHSLA ColorsRGB ColorsRGBA ColorsThe opacity PropertyText EffectsThe text-shadow PropertyThe text-overflow PropertyThe word-wrap PropertyWeb FontsGoogle Web FontsTransformationsTransitionsProperties to TransitionTransition DurationTransition DelayTransition TimingShorthand SyntaxTest Your Knowledg

Joining Tables Together 196 Using Logical Operators 198 MySQL Functions 199 Accessing MySQL via phpMyAdmin 199 Windows Users 199 OS X Users 201 Linux Users 201

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.

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 .

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

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

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.

This tutorial focuses heavily on using MySQL in a PHP environment. It is aimed at teaching those who have web hosts with PHP and MySQL already installed. If you are unsure, please contact your web host. MySQL Setup Guide The easiest way to experiment with MySQL and PHP is to p