Table Of ContentEssential Series
Springer-Verlag London Ltd.
Also in this se ries:
John Cowell David Thew
Essential Visual Basic 5.0 fast Essential Access 2000 fast
3-540-76148-9 1-85233-295-6
Duncan Reed and Peter Thomas lan Palmer
Essential HTML fast Essential Java 3D fast
3-540-76199-3 1-85233-394-4
John Hunt Matthew Norman
Essential JavaBeans fast Essential ColdFusion fast
1-85233-032-5 1-85233-315-4
John Vince lan Chivers
Essential Virtual Reality fast Essential Linux fast
1-85233-012-0 1-85233-408-8
John Cowell Fiaz Hussain
Essential Visual J++ 6.0 fast Essential Flash 5.0 fast
1-85233-013-9 1-85233-451-7
John Cowell John Vince
Essential Java 2 fast Essential Mathematics for
1-85233-07l-6 Computer Graphics fast
1-85233-380-4
John Cowell
Essential Visual Basic 6.0 fast
John Cowell
1-85233-07l-6 Essential VB. NET fast
1-85233-591-2
lan Chivers
Essential Visual C++ 6.0 fast
Aladdin Ayesh
1-85233-170-4
Essential UML fast
1-85233-413-4
John Vince
Essential Computer Animation fast
Fiaz Hussain
1-85233-141-0
Essential Dreamweaver 4.0 fast
Aladdin Ayesh 1-85233-573-4
Essential Dynamic HTML fast
1-85233-626-9
Simon Stoba rt
Essential
PHPfast
Building Dynamic Web
Sites with MySQL
Springer
Simon Stobart, BA (Hons), PhD, MBCS, CEng
School of Computing, Engineering and Technology
University of Sunderland
St Peter's Campus
St Peter's Way
Sunderland SR6 ODD
Series Editor
John Cowell, BSc (Hons), MPhil, PhD
Department of Computer Science, De Montfort University, The Gateway,
Leicester LEI 9BH
British Library Cataloguing in Publication Data
Stobart, Simon
Essential PHP fast: building dynamie Web sites with MySQL.
- (Essential series)
1. MySQL (Computer file) 2. PHP (Computer program language)
3. Web sites - Design
I. Title 11. PHP fast
005.7'2
ISBN 978-1-85233-578-6 ISBN 978-1-4471-0209-0 (eBook)
DOI 10.1007/978-1-4471-0209-0
Library of Congress Cataloging-in-Publication Data
A catalog record for this book is available from the Library of Congress.
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as
permitted under the Copyright, Designs and Patents Act 1988, this publication may only be
reproduced, stored or transmitted, in any form or by any means, with the prior permission in
writing of the publishers, or in the case of reprographie reproduction in accordance with the terms
of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside
those terms should be sent to the publishers.
Essential series ISSN 1439-975X
ISBN 978-1-85233-578-6
http://www.springer.co.uk
© Springer-Verlag London 2002
Originally published by Springer-Verlag London Limited in 2002
The use of registered names, trademarks etc. in this publication does not imply, even in the absence
of a specific statement, that such names are exempt from the relevant laws and regulations and
therefore free for general use.
The publisher makes no representation, express or implied, with regard to the accuracy of the
information contained in this book and cannot accept any legal responsibility or liability for any
errors or omissions that may be made.
Typeset by Mac Style Ltd, Scarborough, N. Yorkshire
34/3830-543210 Printed on acid-free paper SPIN 10863175
For
Liz, Gemma and Hayley
With special thanks to
John Richings and Erling Bjornsen
Contents
1. INTRODUCTION........................................ 1
Introduction 2
WhyPHPandMySOl? 2
IsthisBookforYou? 3
HowtoUsethisBook 4
2. CREATINGA TYPICALPHP DEVELOPMENT
ENVIRONMENT.......................................... 7
Introduction 8
WebBrowser 8
WebServer 10
PHP 15
TextEditor 15
Database 16
Summary 16
3. INTRODUCING PHP.......... 17
Introduction 18
ASimpleHTMlDocument.................................................................................................. 18
FirstPHPScript................................................................................................................... 19
TheHistoryofPHP 22
StaticandDynamicWebPages _......................................................... 22
HowIsPHPDifferent? 23
FormattingPHPInstructions 25
JumpingInandOutofPHP 25
EchoandPrintStatements 26
CommentStatements 27
Summary 28
4. VARIABLES: INTEGER AND FLOATING-POINT
DJlT~ T~IC~ ••....••.........•........••••••••.••••••••••• 31
Introduction 32
WhatIsaVariable? 32
AssigningaVariablebyValue 33
AssigningaVariablebyReference 34
DifferentTypesofVariables 36
Essential PHP fast
Integers.............................................................................................................................. 37
Floating-PointNumbers 39
Summary 41
5. VARIABLES: STRINGS 43
Introduction 44
SingleQuotedStrings......................................................................................................... 44
DoubleQuotedStrings 45
VariablesinStrings............................................................................................................. 46
JoiningStrings 47
ReferencingCharactersinaString...................................................................................... 48
Stringlength 50
ConvertingStrings.............................................................................................................. 51
Summary 53
6. ACCESSING ENVIRONMENT AND PHP
"Jl~I3L~S Sf)
Introduction 56
PredefinedVariables........................................................................................................... 56
EnvironmentVariables........................................................................................................ 57
PHPVariables 60
Summary 61
7. INVESTIGATING EXPRESSIONS,
OPERANDS AND OPERATORS..... 63
Introduction 64
Expressions......................................................................................................................... 65
Operators 66
Operands 66
ArithmeticOperators 67
TheAssignmentOperator 68
BitManipulationOperators 70
ComparisonOperators 73
TheConditionalOperator 73
IncrementandDecrementoperators.................................................................................. 74
logicalOperators................................................................................................................ 76
OperatorPrecedence 77
Summary 79
8. CONTROLLING THE FLOWOF CONTROL.. 81
Introduction 82
Sequence,SelectionandIteration 82
Contents
TheifStatement................................................................................................................. 84
TheelseStatement............................................................................................................. 85
TheelseifStatement 87
Thewhileloop 88
Thedowhileloop 90
Theforloop 91
Nestedloops 94
BreakingOutofloops 96
ThecontinueStatement 97
TheswitchStatement 98
Summary 101
9. INTERACTINGWITH THE USER 103
Introduction 104
PHPandForms................................................................................................................... 104
SimpleFormProcessing 104
GETandPOSTMethods 107
CombiningPHPandForms................................................................................................. 107
RetainingFormFieldData 110
InteractingwithaPHPApplication 113
Summary 115
10. ARRAYS 117
Introduction 118
Single-DimensionalArrays 118
Multi-DimensionalArrays................................................................................................... 121
ArraysandtheforeachLoop............................................................................................... 125
StringsandArrays 126
CombinationArrays 127
ManipulatingArraysatRunTime 129
Summary 131
11. THE MYSQL DATABASE MANAGEMENT
SYSTEM 133
Introduction 134
WhatIsMySQl? 134
StartingtheDatabase 135
CreatingaDatabase 136
DroppingaDatabase 139
HidingtheWinMySQLadminApplication 140
CreatingDatabaseTables.................................................................................................... 140
Essential PUP fast
SelectingaMySQLDatabase 142
CreatingDatabaseTablesfromaDumpFile........................................................................ 145
DroppingMySQLDatabasesandTables 147
QuittingMySQL.................................................................................................................. 147
Summary 147
12. ACCESSING MYSQL DATABASES USING
»III? 1~~
Introduction 150
ASimplePHPScripttoDisplayDatabaseRecords............................................................... 150
AccessingMultipleRecordsbyRecordNumber 153
AccessingMultipleRecordsbyColumnName..................................................................... 154
CheckingfortheExistenceofRecords 156
SearchingforSpecificRecords 157
UsingHyperLinkstoSelectRecords 159
UsingaFormtoSelectRecords 161
UsingaFormtoSearchonDifferentFields......................................................................... 163
AddingRecords 166
DeletingRecords 169
UpdatingRecords 171
Summary 173
13. IMPLEMENTING FUNCTIONS 175
Introduction 176
AdvantagesofFunctions 176
ASimpleFunction 177
MorePowertoFunctions:UsingArguments 180
FunctionswithMultipleParameters 182
FunctionsandLocalVariables............................................................................................. 184
ArgumentsPassedbyValue................................................................................ 185
ArgumentsPassedbyReference......................................................................................... 186
DefaultArgumentValues 188
ReturningaValuefromaFunction 189
FunctionCallsWithinFunctions 192
Summary 194
14. ~()()~~~ . 195
Introduction . 196
LimitationsofCookies .. 196
SettingaCookie . 196
ViewingaCookie .. 198
DeletingaCookie .. 199
Contents
UsingCookiestoPersonalizePages 199
Summary 201
15. FILE HANDLING 203
Introduction 204
OpeningaFile 204
ClosingaFile 205
ReadingCharactersfromaFile 206
TheEndofFileFunction 209
ReadingLinesfromaFile 209
ReadingaRemoteFile 211
WritingtoaFile.................................................................................................................. 213
Summary 214
16. CLASSES AND OBJECTS 215
Introduction 216
ASimpleClass 217
AccessinganObject'sVariables........................................................................................... 220
MultipleObjectsofaClass.................................................................................................. 222
Constructors 224
ArraysofObjects 225
ObjectsinObjects............................................................................................................... 226
Inheritance 230
Summary 233
17. FURTHER INFORMATION RESOURCES 235
Introduction 236
ThePHPManual................................................................................................................. 236
ThePHPOnlineManualandFAQ 237
TheMySQLManual............................................................................................................. 238
TheMySQLOnlineManualandFAQ.................................................................................... 239
OtherOnlineResources 240
AndFinally... 241
I!fJ)It~ ....•......................................................... 2~3