example XML document, DOM tree of,389 DOUBLE data type, 171 DROP keyword, 176 DROP TABLE command, issuing from PHP, 240 duplication of data across multiple database columns, 204 across m
Trang 1example XML document, DOM tree of,
389
DOUBLE data type, 171
DROP keyword, 176
DROP TABLE command, issuing from PHP,
240
duplication of data
across multiple database columns, 204
across multiple database rows, 206
dynamic linking (PHP), 85
use by WordPress blogging platform, 86
dynamic web content, 1–11
Apache web server, 8
benefits of PHP, MySQL, and JavaScript, 5–
8
combination of PHP, MySQL, and
JavaScript, 9
HTTP and HTML, 2
E
each function, using with list function to walk
through associative array, 120
EasyPHP, 14–18
downloading and installing, 14
overcoming installation problems, 14
testing installation, 16
echo <<< construct, 252
echo command (PHP), print command versus,
51
Editra program editor, 29
else statements
in JavaScript, 329
in PHP, 72
closing if else or if elseif else
statements, 74
elseif statements (PHP), 73
positioning and number of, 74
email address, validating in form input, 360
empty object, creating in JavaScript, 343
encapsulation, 100
end function, using with PHP arrays, 128
endswitch command, replacing final curly
brace in switch statement, 77
equality operators
in JavaScript, 323
in PHP, 67
ereg_replace function (PHP), 149
errors
error text from last called MySQL function
in PHP, 227 JavaScript
catching using onError, 326 catching using try catch, 327 debugging, 303
trapping in for loop using break statement,
83 trapping in for loop using continue
statement, 84 escape characters
backslash (\) in regular expressions, 363
in JavaScript, 310
in PHP, 47 preventing in strings for submission to
MySQL, 263 escapeshellcmd function (PHP), 150 European Laboratory for Particle Physics
(CERN), 1 event-min.js file, 396 exclusive or (xor) operator, 44 exec system call (PHP), 149 arguments, 150 execution (` `) operator, 63 EXPLAIN tool (MySQL), 217 explicit casting, 85
JavaScript and, 334 explode function, using to create PHP arrays,
125 expressions JavaScript, 319–321 literals and variables, 320 operators, 321–325 PHP, 61
literals and variables, 62 operators, 63–70 extends operator (PHP), 109 Extensible Hypertext Markup Language (see
XHTML) extract function, using with PHP arrays, 125
F
\f (form feed) in JavaScript strings, 310 fclose function (PHP), 138
fgets function (PHP), 138, 141 reading from files, 139 file handling in PHP, 137–149 checking if file exists, 137 copying files, 139
Trang 2creating a file, 137
deleting a file, 140
locking files, 142
moving a file, 140
reading entire file, 143
reading from files, 139
updating files, 141
uploading files, 144–149
form data validation, 147
using $_FILES array, 146
file pointer, 141
$_FILES array, 145
contents of, 146
files, including and requiring in PHP, 96
include statement, 96
include_once, 97
require and require_once, 97
FileZilla, 28
file_exists function, 137
file_get_contents function (PHP), 143
final methods (PHP), 112
finally clause (try catch), 328
Firefox
accessing JavaScript error messages, 303
Error Console message for JavaScript errors,
304
Firebug plug-in, 305
FireFTP, 27
FireFTP
advantages of, 27
installing, 27
fixation, session, 294
FLOAT data type, 171
flock function (PHP), 142
filesystems not supporting and use on
multithreaded server, 143
unlocking files, 143
fopen function (PHP), 138
supported modes, 138
for loops
in JavaScript, 332
breaking out of, 333
continue statement, 334
in PHP, 81
breaking out of, 83
continue statement, 84
controls removed from body of loop, 82
when to use, while loops versus, 82
foreach as loops, 119–120
printing out values in multidimensional
associative array, 122 walking through multidimensional numeric
array, 123 forEach method (JavaScript), 349 cross-browser solution, 350 foreign keys, 206
form feed (\f) in JavaScript strings, 310
<form> tag, onSubmit attribute, 357 forms, 251–267
building using PHP, 251 creating form to add records to MySQL
database, using Smarty, 272 data validation, 147
example PHP program converting between
Fahrenheit and Celsius, 264–266 inserting and deleting data in MySQL using
PHP (example), 232–236 processing submitted data using PHP, 253–
264 checkboxes, 257 default values, 255 hidden fields, 260 input types, 256 labels, 262 radio buttons, 259 sanitizing input, 263 select tags, 260 text areas, 256 text boxes, 256 redisplaying after PHP validation, 370–375 uploading files from, 144–149
validating user input with JavaScript, 355–
361 form field validation, 358–361 frameworks for JavaScript, 393, 394 (see also YUI)
fread function (PHP), 138 reading a file, 139 friends on social networking site adding and dropping, 424 module showing user’s friends and
followers, 427–430 fseek function (PHP), 141 FTP, transferring files to and from web server,
27 FULLTEXT indexes, 182 stopwords, 457–459 using MATCH AGAINST on, 188
Trang 3defined, 89
JavaScript, 312, 337–341
defining, 337
prototype property, 344
returning a value, 339
returning an array, 341
MySQL, 194
commonly used, reference listing, 461–
472
PHP, 52, 90–96
array functions, 123–128
defining, 91
passing by reference, 94
returning a value, 92
returning an array, 93
version compatibility, 98
social networking site project, 407–409
fwrite function (PHP), 138
G
/g (global matching) in regular expressions,
369
$_GET and $_POST arrays, sanitizing user
input, 263
GET method, 380, 397
Ajax GET example using YUI, 397
using instead of Ajax POST, 385–387
getElementById function (JavaScript), 316
get_post function (PHP), 234
global matching in regular expressions (/g),
369
global variables
JavaScript, 312
PHP, 55
returning from function calls, 95
Glossword WAMP, 18
Gmail, use of Ajax to check for username
availability, 10
Google Chrome
accessing JavaScript error messages, 303
Error Console message for JavaScript error,
304
Google Maps, 377
GRANT command, example parameters for,
165
GROUP BY command, 191
H
head section of HTML document, using
JavaScript within, 301 heredoc (<<<) operator, 48 hexadecimals, escaping in JavaScript strings,
310 hidden fields in forms, 260 history object (JavaScript), 317 HTML, 2, 151
(see also XHTML) basic knowledge of, xiii DOM (Document Object Model) in
JavaScript, 314–318 incorporating PHP within, 33 JavaScript and, 300
comment tags for older and nonstandard
browsers, 301 debugging JavaScript errors, 303 including JavaScript files, 302 using scripts within document head,
301 left arrow (← entity), 417 manipulation with JavaScript, 7 multiline output, creating in PHP, 252 program editors for, 29
sanitizing in form input, 263 HTML 4.01 document types, 153 HTML injections, 248
<html> tag, xmlns attribute, 153 htmlentities function (PHP), 57, 249 HTTP, 2
request/response procedure, 2 HTTP authentication, 282–288 checking for valid username and password
using PHP, 284 login prompt, 282 storing usernames and passwords, 285 creating users table in MySQL using
PHP, 286 PHP authentication using MySQL, 287 salting passwords, 285
user clicks Cancel before logging in, 283 HTTPS, 293
I
identity operator (see ===, under symbols) IDEs (Integrated Development Environments),
30
Trang 4PHP IDEs, listed, 32
if else statements (PHP), 72
if statements
in JavaScript, 328
else statements, 329
in PHP, 71
OR operator, problems with, 70
image media types, 149
images, uploading as form data in PHP, 144
implicit casting, 84
in keyword (JavaScript), 347
include statements in PHP, 96
include_once function (PHP), 97
increment operator (++), 42, 45
in JavaScript, 308
using in while loop, 80
variable incrementing in JavaScript, 310
incrementing
using AUTO_INCREMENT in MySQL,
172
variable incrementing in JavaScript, 310
variables in JavaScript, 310
index.php file (social networking site project),
411
indexes, 206
arrays in PHP, 40
associative arrays, 117
numerically indexed, 115
MySQL tables, 177–183
adding when creating tables, 179
creating using ALTER TABLE, 178
creating using CREATE INDEX, 179
FULLTEXT index, 182
performance and, 182
PRIMARY KEY, 180
types of indexes, 178
using MATCH AGAINST on
FULLTEXT index, 188
inheritance
defined, 100
PHP classes, 109–113
initialization expression (for loop), 81
ini_set function (PHP), 293
session data on shared server, 296
InnoDB storage engine, 215
input, forms
sanitizing, 263
types of input, 256
INSERT command, 7
adding data to table, 174 insert IDs, 243
instance of a class, 342 INT data type, 171 INTEGER data type, 171 Integrated Development Environments (see
IDEs) interface, PHP objects, 100 Internet Explorer
accessing JavaScript error messages, 303 compatibility issues, JavaScript program
testing and, 305 Error Console message for JavaScript error,
304 forEach method and, 350 incompatibilities caused by JScript, 316 XMLHttpRequest object, IE 6 and, 378 Internet media types, 147
Internet, early history of, 1 intval function (PHP), 266
IP addresses, 3 storing for users, 293
is system command, 149 isNAN function (JavaScript), 360 isset function (PHP), 254 is_array function, 123
J
JavaScript, 299–318, 377 (see also Ajax) arrays, 346–353 benefits of, 5 combination with PHP and MySQL for
dynamic content, 9 comments, 305
conditionals, 328–331 DOM (Document Object Model), 314–318 explicit casting and, 334
expressions, 319 frameworks for, 393, 394 (see also YUI) functions, 312, 337–341 global variables, 312 HTML and
debugging JavaScript errors, 303 including JavaScript files, 302 older and nonstandard browsers,
comment tags, 301
Trang 5using scripts within document head,
301
HTML text and, 300
injection via malicious form input, 263
local variables, 312
looping, 331–334
objects, 341–346
onError event, 326
operators, 308–311, 321–325
regular expressions, 361–369
using, 369
semicolon (;) ending statements, 305
try catch statements, 327
using, 7
validating user input, 355–361
variable typing, 311
variables, 306–307
with statement, 325
XSS injections, 248
JOIN ON construct in MySQL, 194
join method (JavaScript), 350
joining tables in MySQL, 192–194
AS keyword, 194
JOIN ON construct, 194
NATURAL JOIN, 193
JScript, 316
K
keys, 168, 181
(see also indexes)
purposes of keys in MySQL, 206
keys and values, numerically indexed PHP
arrays, 117
L
labels in forms, 262
LAMPs (Linux, Apache, MySQL, and PHP),
13
installing on Linux, 25–26
LIKE qualifier (MySQL), 186
link URL, reading with JavaScript, 315
linking, dynamic linking in PHP, 85
links object (JavaScript), 317
Linux
accessing MySQL via command line, 161
accessing MySQL via phpMyAdmin, 195
installing LAMP, 25–26
installing MDB2 package, 477
installing other PEAR packages, 481 installing PHPUnit, 482
likely location for mysqldump, 219 system calls from PHP, 149 Linux, Apache, MySQL, and PHP (see LAMPs) list function, using with each function to walk
through associative array, 120 literals, 320
PHP, 62 local variables JavaScript, 312 PHP, 53 locking files in PHP, 142 log out page (social networking site project),
433 logical operators
in JavaScript, 309, 324
in MySQL queries, 194
in PHP, 44, 69 login page, social networking site project, 418 login.php file, creating, 226
looping
in JavaScript, 331–334 breaking out of loops, 333 continue statement, 334
do while loops, 332 for loops, 332
while loops, 331
in PHP, 78–84 breaking out of loops, 83 continue statement, 84
do while loops, 81 for loops, 81
foreach as loop, 119–120 while loops, 78
M
/m (multiline mode) in regular expressions,
369 Mac OS X accessing MySQL via command line, 160 accessing MySQL via phpMyAdmin, 195 FTP on, 28
installing a MAMP, 19–25 versions of OS X prior to 10.3, 25 installing other PEAR packages, 481 installing PEAR, 475
installing PHPUnit, 482 likely location for mysqldump, 219
Trang 6system calls from PHP, 149
Macintosh, SSH on, 27
MAMPs (Mac, Apache, MySQL, and PHP), 13
installing on Mac OS X, 19–25
alternative MAMPs, 25
configuring ports, 24
many-to-many relationships, 213
MATCH AGAINST queries, 188
using Boolean mode, 189
md5 function (PHP), 285
md5 hexadecimal strings, 294
MDB2 package (PEAR), 473
creating connect instance, 477
installing on Linux or Unix, 477
installing on Mac OS, 475
installing on Windows, 474
querying database, 478
using to rewrite sqltest.php program
(example), 479
MEDIUMINT data type, 171
members module, social networking site
project, 424–427
adding and dropping friends, 424
listing all members, 424–427
viewing a user’s profile, 424
messaging module, social networking site
project, 430–433
metacharacters (regular expressions), 361
escaping, 363
summary of, 367
method chaining, 340
methods
defined, 99
JavaScript objects, 314, 342
static methods, 345
using prototype keyword for a method,
344
PHP objects, 89
calling, 102
scope, controlling in PHP 5, 107
writing, 105
Microsoft Internet Explorer (see Internet
Explorer)
Microsoft JScript, 316
{min,max} matching in regular expressions,
368
mktime function (PHP), 134
modification expression (for loop), 81
move_uploaded_file function (PHP), 146
Mozilla Firefox (see Firefox) multidimensional arrays
in JavaScript, 348
in PHP, 121–123 multiline mode in regular expressions (/m),
369 multipart/form-data encoding, 144 multiple-line commands in PHP, 47 MyISAM ENGINE, 167
MySQL accessing remotely, 27 accessing via command-line, 158–177 accessing via command-line interface
on remote server, 162 accessing via phpMyAdmin, 195 adding data to table using PHP, 240 AUTO_INCREMENT, using from PHP,
243 backing up and restoring data, 219–223 benefits of, 5
combination with PHP and JavaScript for
dynamic content, 9 creating form to add records to database,
using Smarty, 272 creating table using PHP, 238 creating users table and adding accounts
using PHP, 286 data retrieval from table using PHP, 241 database design, 201
database terms, summary of, 158 deleting data from database using PHP,
242 describing table using PHP, 239 dropping table using PHP, 240 example of simple database, 157 EXPLAIN tool for queries, 217 FULLTEXT stopwords, 457–459 functions, 194
commonly used, reference listing, 461–
472 indexes for tables, 177–183 inserting and deleting data using PHP
(example), 232–238
$_POST array, 234 deleting a record, 235 displaying form, 236 querying database, 236 running program, 237 joining tables, 192–194
Trang 7normalization, 203
PEAR MDB2 package (see MDB2 package)
performing secondary query using PHP,
244
PHP authentication using, 287
preventing escape character injection into
string presented to MySQL, 263
preventing HTML and XSS injections, 248
preventing SQL injection, 245–248
querying a database, 183–192
querying database with PHP, 225–231
connecting to MySQL, 227–231
creating login file, 226
steps in process, 225
relationships among data, 211–214
sanitizing in form input, 263
social networking site project
tables setup file, 410
transactions, 214–217
updating data using PHP, 242
using, 6
mysqldump utility, 219
dumping backups to file, 220
dumping data into CSV format files, 222
locations on various installations and
operating systems, 219
mysql_close function (PHP), 231
mysql_connect function (PHP), 227
mysql_entities_fix_string function (PHP), 249
mysql_error function (PHP), 227
mysql_fetch_array function (PHP), 274
mysql_fetch_row function (PHP), 231
mysql_insert_id function (PHP), 244
mysql_result function (PHP), 229
N
\n (newline character), 367
in JavaScript strings, 310
in PHP strings, 47
{n} matches exactly n times in regular
expressions, 368
{n, } matches n times or more in regular
expressions, 368
naming convention for JavaScript functions,
338, 342
NATURAL JOIN in MySQL, 193
new operator in JavaScript, 322
normalization, 203–211
First Normal Form, 204
Second Normal Form, 206–208 Third Normal Form, 209–210 when not to use, 210
<noscript> </noscript> tags, 300 not operator (!), 44, 309
NOT operator (!), 69 NOT operator (in MySQL), 194 NULL values, representing FALSE, 69 numbers
converting PHP strings to and from, 49 converting strings to and from in JavaScript,
311 numeric arrays
in PHP, 115 multidimensional, 122 walking through, using foreach as,
119 JavaScript, 346 assigning element values, 346 assignment using Array keyword, 347 numeric data types (MySQL), 170
numeric variables JavaScript, 307 PHP, 39
O
objects defined, 89 DOM (Document Object Model) in
JavaScript, 314–318 JavaScript, 341–346
creating, 343 declaring a class, 342 prototype keyword, 344 PHP, 98–113
accessing, 101 cloning, 102 constructors, 104 creating, 101 declaring a class, 100 declaring constants, 107 declaring properties, 106 inheritance and extension of classes,
109–113 property and method scope in PHP 5,
107 static methods in PHP 5, 105 terminology associated with, 99 writing methods, 105
Trang 8octals, escaping in JavaScript strings, 310
one-to-many relationships, 212
one-to-one relationships in data, 211
one-way functions, 285
onError event (JavaScript), 326
open source, 9
Opera browsers
accessing JavaScript error messages, 303
Error Console message for JavaScript, 304
Error Console message for JavaScript error,
304
operating systems, 13
(see also individual operating system names)
system calls in PHP, 149
operators
JavaScript, 308–311, 321–325
arithmetic, 308
assignment, 308
associativity, 322
comparison, 309
escaping characters, 310
logical, 309
precedence of, 321
relational, 323
string concatenation, 310
types of, 321
PHP, 42, 63–70
arithmetic, 42
assignment, 43
associativity, 66
comparison, 43
logical, 44
operator precedence, 64
precedence of, 64
relational, 67–70
types of, 63
or operator
low-precedence or, 44
precedence in PHP, 65
||, 44, 309
OR operator, 69
causing problems in if statements, 70
OR operator (in MySQL), 194
ORDER BY command, 191
P
page design and layout file (social networking
project), 409
parent operator (PHP), 110
parsers calling PHP parser, 34 passing by reference, 94 pattern matching, 361 (see also regular expressions) doing with LIKE qualifier in MySQL, 186 PEAR (PHP Extension and Application
Repository), 473–481 creating connect instance, 477 installation, 473–477
installing other PEAR packages, 481 querying database, 478
rewriting sqltest.php program (example)
using MDB2, 479 Perl, 5
PHP, 33–59, 269 (see also Smarty templating system) adding data to MySQL table, 240 benefits of, 5
casting, implicit and explicit, 84 code examples from this book, 35 combination with MySQL and JavaScript
for dynamic content, 9 comments, 36
conditionals, 70–78 constants, 50 creating forms, 251 creating table in MySQL, 238 deleting data from MySQL database, 242 describing table in MySQL, 239
determining web server’s document root,
270 difference between echo and print
commands, 51 dropping table in MySQL, 240 dynamic linking, 85
echo <<< statement, 48, 252 expressions, 61
form handling creating forms, 251 program converting between Fahrenheit
and Celsius, 264–266 retrieving submitted data, 253–264 sanitizing input, 263
form validation, redisplaying form after,
370–375 functions, 52 IDEs, listing of, 32 incorporating within HTML, 33
Trang 9calling PHP parser, 34
inserting and deleting data in MySQL
(example), 232–238
$_POST array, 234
deleting a record, 235
displaying form, 236
querying database, 236
running program, 237
inserting and deleting data in MySQL table,
deleting a record, 235
multiple-line commands, 47
operators, 42–45, 63–70
arithmetic, 42
assignment, 43
comparison, 43
logical, 44
performing secondary query on MySQL
table, 244
phpDesigner IDE, 30
preventing HTML and XSS injections in
MySQL, 248
preventing SQL injection in MySQL, 245–
248
using placeholders, 248
program editors for, 29
querying MySQL database, 225–231
building and executing query, 228
closing connection, 231
connecting to MySQL, 227
creating login file, 226
fetching result, 229
selecting a database, 228
steps in process, 225
regular expressions in, 369
retrieving data from MySQL table, 241
semicolon (;) ending PHP statements, 37
support by Apache web server, 9
updating data in MySQL table, 242
using, 5
using AUTO_INCREMENT in MySQL
table, 243
using cookies, 279–282
accessing a cookie, 281
destroying a cookie, 282
setting a cookie, 281
using for Ajax
program for Ajax GET request, 397
urlget.php program (example), 398
using HTTP authentication, 282–288
checking for valid username and
password, 284 login prompt, 282 storing usernames and passwords, 285 using in social networking site (see social
networking site, creating) using sessions, 289–296
ending a session, 292 session security, 293–296 starting a session, 289–291 using with Ajax
urlget.php program (example), 386 urlpost.php program (example), 383 xmlget.php program (example), 387 variable assignment, 45–47
escaping characters in strings, 47 incrementing and decrementing variable,
45 string concatenation, 46 string types, 46 variable scope, 53–58 variable typing, 49 variables, 38–42 arrays, 39 naming rules, 42 numeric, 39 syntax, 37 two-dimensional arrays, 40 version compatibility, checking for
functions, 98 PHP 5
constructor methods, creating, 104 destructor methods, creating, 104 property and method scope, 107 static methods, 105
PHP Extension and Application Repository (see
PEAR) php file extension, 33 phpinfo function, 90 phpMyAdmin, 195 accessing MySQL from Linux, 195 accessing MySQL from Mac OS X, 195 accessing MySQL from Windows, 195 using, 197
PHPUnit, 481–484 manual for, 484 rules for writing tests, 484 phpversion function, 98 placeholders
Trang 10using to prevent SQL injection in MySQL,
247
pop method (JavaScript arrays), 350
ports, configuring for MAMP on Mac OS X,
24
$_POST array, 234
POST method, 145
Ajax program, urlpost.html (example), 380–
385
making Ajax POST request, 382
<pre> </pre> tags
forcing monospaced font, 236
<pre> </pre> tags, 122, 123
precedence, operator, 64
in JavaScript, 321
precision, setting for printf display, 131
predefined constants (PHP), 51
preg_match function (PHP), 369
preg_match_all function (PHP), 369
preg_replace function (PHP), 369
primary keys, 180
adding to table retrospectively, 181
adding when creating tables, 181
importance in database design, 202
print command (PHP), echo command versus,
51
printf function (PHP), 129–133
conversion specifiers, 129
setting precision for displayed results
conversion specifier components, 131
setting precision of displayed result, 131
string padding, 132
print_r function, 101
privacy, databases and, 214
private keyword (PHP 5), 108
profiles, social networking site project, 419–
424
adding profile image, 420
adding “About Me” text, 420
displaying current profile, 421
processing image, 420
viewing another member’s profile, 424
program editors, 29
properties
defined, 99
JavaScript objects, 314, 342
adding new, 343
prototype property, 344
static properties, 345
PHP objects accessing, 102 declaring, 106 scope, controlling in PHP 5, 107 protected keyword (PHP 5), 108 prototype keyword (JavaScript), 344 extending objects, 345
static methods and properties, 345 prototype property (JavaScript functions), 344 pseudofunctions (constructs), 90
public keyword (PHP 5), 108 push method (JavaScript arrays), 346, 350 PuTTY program, for Telnet and SSH access on
Windows, 27
Q
querying MySQL database, 183–192, 225 (see also PHP, querying MySQL database) building and executing query from PHP,
228 DELETE command, 184 EXPLAIN tool for queries, 217 grouping results with GROUP BY, 191 LIMIT qualifier, 187
logical operators in WHERE queries, 194 MATCH AGAINST construct, 188 SELECT command, 183
SELECT COUNT command, 183 SELECT DISTINCT command, 184 sorting results with ORDER BY, 191 UPDATE SET construct, 190 using PHP, 236
WHERE keyword, 185 quotation marks, 38 (see also listings under Symbols section) escaping in JavaScript strings, 310
in JavaScript strings, 306
R
\r (carriage return)
in JavaScript strings, 310
in PHP strings, 47 radio buttons, 258 radio buttons in forms, 259 ranges, indicating in regular expressions, 364 readyState property (XMLHttpRequest), 382 REAL data type, 171
reference books, xiv