PHP andMySQL
The book that should have been in the box
Brett McLaughlin
Beijing | Cambridge | Farnham | Kln | Sebastopol | Tokyo
2013
PHP MySQL
PHP andMySQL
The book that should have been in the box
Brett McLaughlin
Beijing | Cambridge | Farnham | Kln | Sebastopol | Tokyo
2013
PHP MySQL
32.988-02-018 004.738.515
.15 PHP MySQL. . .: , 2013. 512 .: . ISBN 978-5-459-01550-8
CSS JavaScript, - - PHP MySQL. - . , , , -.
PHP- -. PHP SQL. . -, . , . ,
. .
32.988-02-018 004.738.5
'Reilly. . .
, , , -. , , , .
ISBN 978-0596515867 . 2012 Brett McLaughlin. All rights reserved
ISBN 978-5-459-01550-8 , 2013
,
, 2013
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1. PHP MySQL 1. PHP: , ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2. PHP: . . . . . . . . . . . . . 50 3. MySQL SQL: . . . . . . . . . . . . . . . . . . . . . . . . 82
2. - 4. PHP MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 5. . . . . . .157 6. - . . . . . . . . . . . . . . . . . . . . . .174
3. - - 7. - ( ) . . . . . . . .222 8. . . .260 9. . . . . . . . . . . . . . . . .294 10. , . . . . . . . . . . .337
4. 11. . . . . . . . . . . . . . . . . . . . . . . . . .392 12. Cookie-,
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .428 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 PHP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
PHP . . . . . . . . . . . . . . . . . . . . . . . . . . 15PHP , JavaScript . . . 17PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
MySQL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Macintosh Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19FTP: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1. PHP MySQL
1. PHP: , ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
PHP (PC) . . . . . . . . . . . . . . . . . . . . . . . 27PHP Macintosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
: HTML- . . . . . . . . . . . . . . . . . . . . . 42 PHP- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
HTML, CSS PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2. PHP: . . . . . . . . . . . . . 50 - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
. . . . . . . . . . . . . . . . 50 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 $_REQUEST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
. . . . . . . . . . . . . . . . . . 74 $_REQUEST . . . . . . . . . . . . . . . . . . . . . . . . . 76 ? . . . . . . . . . . . . . . . . . . 80
3. MySQL SQL: . . . . . . . . . . . . . . . . . . . . . . . . 82 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
. . . . . . . . . . . . . . . . 82 . . . . . . . . . . . . . . . . . 84() . . . . . . . . . . . . . . . . 86
MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86MySQL Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88MySQL Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 SQL- . . . . . . . . . . . . . . . . . . . . . . . . . .100
SQL . . . . . . . . . . . . . . . . . . . . . . . . .105 - . . . . . . . . . . . . . . . . . . . . . . .107 USE . . . . . . . . . . . .109 CREATE . . . . . . . . . . . . . . . .110 DROP . . . . . . . . . . . . . . . . . . .114 INSERT . . . . . . . . . . . .115 SELECT . . . . . . . . . . . . . . . . . . . . . . . . . .116
8
2. -
4. PHP MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 PHP-, . . . . .120
MySQL . . . . . . . . . . . . . . . . . . . . . . . . . .121 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
SHOW . . . . .127 . . . .128 SQL- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
. . . . . . . . . . . . . .133 . . . . . . . . . . . . .134
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 , . . . . . .137 SQL- . . . . . . . . . . . . .140 HTML- . . . . . . . . . . .140 ( ) . . . . . . . . . . . . . . . . . . . . . . . .142 SQL- ( ) . . . . . . . . . . . . . . . .143 ,
- . . . . . . . . . . . . . . . . . . . . . . . . . . .145 ,
SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
5. . . . . . .157 , . . . . . . . . . . . . . . . . . . . . . . . . . . . .158
. . . . . . . . . . . . . . . . . . . . . . . . . .158 ... . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 trim strtoupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 : . . . . . . . . . . .172
6. - . . . . . . . . . . . . . . . . . . . . . .174 . . . . . . . . . . . . . . . .174 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
id . . . . . . . . .177 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178
9
ID . . . . . . . . . . . . . . . . .178 . . . . . . . . . . . . . . . . . . . . . .180
. . . . . . . . . . . . . . . . . . . . . . . . . . . .182 SQL- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 . . . . . . . . . . . . . . . . . . . . . . . . .188 . . . . . . . . . . . . . .189
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 . . . . . . . . . . . . . . . .191 ALTER . . . . . . . . . . . . . . . .194 : . . . . . . . . . . . . . . . . . . . . . . . . . .196
SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . .201 SQL- . . . . . . . . . . . . . . . .204 ID show_user .php . . . . . . . . . . . .207
, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210
. . . . . . . . . . . . . . . . . . . . . . . . . . . .210 . . . . . . . . . . . . . . . . . .213
( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . .216
3. - -
7. - ( ) . . . . . . . .222 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224
? . . . . . . . . . . . . . . . . . . . . . . . . . . .225 , . . . . . . . . . . . . . . . . .228
PHP . . . . . . . . . . . . . . . .230 PHP . . . . . . . . . . . . . . . . . . . . . .231 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 . . . . . . . . . . . .237
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 ,
PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241
10
require require_once . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 , . . . . . . . . . . . . . . . . . . . . . . . . .249
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 show_error .php . . . .251 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254 . . . . . . . . . . . . . . . . . . . . . . . .256
8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
HTML . . . . . . . . . . . . . . . . . . . . . . . . . .263 . . . . . . . . . . . . .266 ? . . . . . . . . . . . . . . . . . . . . . . . .270 . . . . . . . . .279
, . . . . . . . . . . . . . . . . . . . . . . .282 SELECT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 URL- . . . . . . . . . . .285 : . . . . . . . . . .289 app_config .php . . . . . . . . . . . . .291
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293
9. . . . . . . . . . . . . . . . .294 . . . . . . . . . . . . . . . . . . . . .294 . . . . . . . . . . . . . . . . . . . . .297
getimagesize . . . . . . . . . . . . . .300 file_get_contents . . . . . . . . . . . .300 INSERT . . . . . . . . . . . . .300
. . . . . . . . . . . . . . . . . . .301 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302 ID . . . . . . . . . . . . .305
. . . . . . . . . . . . . . . . . . . . . . . . . .308 . . . . . . . . . . . . . . . .310 WHERE . . . . . . . . . . . . . . . . .316
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328
11
, . . . . . . . . . .329 . . . . . . . . . . . . . . . . . .329
(src) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330, ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335
, ... . . .335
10. , . . . . . . . . . . .337, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .338
, - . . . . . . . . . . . . . . . . . . . . . . . . . . . . .338
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .340 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .341
SELECT ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342
. . . . . . . . . . . . . . . .344 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350 . . . . . . . . . . . . . . . . . . . . . . . . . . . .351
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354, . . . . . . . . . . . . . . . . . . . .358
. . . . . . . . . . . . . . . .359 ,
JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .362 alert . . . . . . . . . . . . . . . . . . . . . . . . . . .368
. . . . . . . . . . . . . . . . . . . . . . . . .369 . . . . . . . . . . . .372 . . . . . . . . . . .374 View Display . . . . . . . . . . . .376
, . . . . . . . . . . . . . . . . . . . . . .377 View . . . . . . . . . . . . . . . . . .377 . . . . . . . . . . . . . . . . . . . . . . . . .378
View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .384 . . . . . . . . . . . . . . . . . . .386
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .387
12
4.
11. . . . . . . . . . . . . . . . . . . . . . . . . .392 . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
HTTP- . . .394 ... . . . . . . . . . . .395 . . . . . . . . . . . . . . . . . . . . . . .396 . . . . . . . . . .397 . . . . . . . . . . . . . . . . . . . . .398 ( !) . . . .399
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403 : authorize .php . . . . . . . . . . . . . . . . . . .403
PHP . . . . . . . . . . . . . . . . . . . . . .407 users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .408 . . . . . . . . . . . . .409 . . . . . . . .411 . . . . . . . . . . . . . . . . . . . . . . . .415 authorize .php users . . . . . . . . . . . . .419
, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422
crypt . . . . . . . . . . . . . . . . . .423 crypt . . . . . . . .424 . . . . . . . . . . . . . . . . . . . . . . . . .425
12. Cookie-, . . . . . . . . . . . . . . . . . . . . . . . . . . . .428 . . . . . . . . . . . . . . . . . . . . . . . .429
. . . . . . . . . . . . . . . . . . . . . . .430 . . .432 HTTP- ookie- . . . . . . . . .435
cookie- . . . . .436 ? . . . . . . . . . . . . . . . . . . . . .438 ? . . . . . . . . . . . . . . . . .438 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .440 . . . . . . . . . . . . . . . . . . . . .442 . . . . . . . . . . . . . .444 cookie- . . . . . . .447 . . . . . . . . .450 . . . . . . . . . . . . . . . . . . . . . . . . . . .454
13
- . . . . . . . . . . . . . . . . . . . . . . . . . . . .455 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456 HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459 . . . . . . . . . . . . . . . . . . . . . . . . . .463 cookie- . . . . . . . . . . . . . . . . . . . . . . . . . .465
13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470 . . . . . . . . . . . . . . . . . . . . . . . . . . . .471 . . . . . . . . . . . . . . . . .475
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .476 authorize .php . . . . . . . . . . . . . . . . . .477 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .479 . . . . . . . . . . . . . . . . . . . . . . . . . . .481, , . . . . . . . . . . . . . . . . . . . . . . . . .485
, . . . . . . . .487 . . . . . . . . . . . . . . . . . .491
. . . . . . . . . . . . . . . . . . . . . .494 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494 $_COOKIE $_SESSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495 . . . . . . . . . . . . . . . . . . . . .496 $_REQUEST
$_SESSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .500 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . .502 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .503
? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504 cookie-? . . . . . . . . . . . . . . . . . . . . . . . . .507
- HTML. Cascading Style Sheets (CSS) aaScit -aaScit - - -- -. aaScit ey - aaScit ey - aaScit ey -aaScit ey - ey -ey - - -. aaScit CSS HTML .
. -
- cookie-. - PayPal . HTML CSS aaScit.
PHP MySL . PHP - - . .
PHP?PHP . aaScit - . HTML --. PHP - -PHP - - - - : .
15
PHP?PHP . PHP Personal Home Page ( ), - -, - HTML, CSS JavaScript. Personal Home Page , , - , -, .
PHP PHP: Hypertext Preprocessor (PHP: ). , , - : PHP , . , , . , , . , PHP - , PHP.
PHP - . PHP -PHP - - ( . 0.1) PHP .
. 0.1. PHP- Windows
PHP HTML- - cookie-. . HTML- HTML PHP.
16
PHP HTML - PHP-:
? HTML -HTML - - (. 0.2). PHP HTML. . . HTML. PHP -HTML. PHP -. PHP -PHP - - : HTML aaScit PHP .
. 0.2. HTML
17
PHP , JavaScript
aaScit aaScit . - var. . - .
PHP aaScit . aaScit. PHP . ... ... .
. . PHP -PHP - - - . - Intenet Exloe aaScit.
PHP PHP- . PHP -PHP - - () - - HTML- -HTML- -- - - . .
aa C++ . : .
PHP aaScit . - . PHP. . . .
18
MySQL?MySL . - . - . MySL SL ( MySL) -SL ( MySL) - ( MySL) -MySL) -) - . MySL 3 PHP.
MySL -MySL - - . MySL - . () MySL. .
PHP - . ( ) - PHP-. ? - -. . - .
PHP. PHP- ... ... -. - PHP -PHP - - .
. - - . HTML CSS aaScit. - - . - - .
19
Macintosh Windows PHP MySL Macintosh Windows . - - . - -.
PHP-. : Macintosh Windows. . HTML CSS.
FTP: TP-. -TP-. --. - PHP- -PHP- -- - vi emacs.
. , , , , .
FTP-. , .
1 TP-. -TP-. --. - Cybedck (www.cyberduck.ch). . TP- - . .
.
1. PHP MySQL. - PHP - PHP -
20
. MySL .
2. -. - -. . URL- Twitte . . - .
3. - -. - . - - . .
4. . - . ( -). cookie- - - .
- Missing Manal (www.missingmanuals.com/cds/phpmysqlmm) - .
. - . - ( ) . www.missingmanuals.com .
- - . www.missingmanuals.com/cds/phpmysqlmm . . .
21
-. - - .
? ? ? - PHP MySL - . -PHP MySL - . - MySL - . -MySL - . - - . - www.missingmanuals.com/feedback.
(Bett McLaghlin) - . - .
- . - NASA . PHP MySL...
(Nan Babe) Missing Manal . - . : [email protected].
(asine Pee) -asine Pee) - Pee) -Pee) -) - - - WMU . : [email protected].
(Nan Reinhadt) -Nan Reinhadt) - Reinhadt) -Reinhadt) -) - - - . . www.nanreinhardt.com. : [email protected].
(Shelley Powes) HTML5 OReilly. . -: www.burningbird.net.
(Stee Sehing) . ( ) - www.braingia.org.
23
. - . - . . - .
. (Bian Sawye) . : . (Nan Babe). . - - .
(Shelley Powes) (Stee Sehing) . . . ... PHP. -. - - .
OReilly. . . - - - - . .
[email protected] ( ).
! http://www.piter.com
.
1
PHP MySQL
1. PHP: , ?
2. PHP:
3. MySQL SQL:
1 PHP: , ?PHP - -. - PHP PHP PHP -.
. PHP 2 .
: ? . PHP -. . PHP HTML-- HTML--HTML----.
PHP . - - PHP- . PHP .
PHP - -PHP - - - - . PHP- . PHP -PHP - -. .
27 1. PHP: , ?
PHP (PC) - . PHP. : PHP -PHP - - .
Macintosh, . PHP Macintosh .
- - www.php.net. - PHP -PHP - - PHP -PHP - - PHP-. PHP Stable Releases (. 1.1).
. 1.1. PHP - www.php.net
. (- .)
PHP -PHP - - PHP (. 1.2).
28 1 . PHP MySQL
- .
. 1.2. - PHP
PHP Windows Binaries ( Windows). PHP Windows-. : http://windows.php.net/download (. 1.3).
http://windows.php.net/download - . : (Non Thread Safe) (Thread Safe). Non Thread Safe ( ) . ( .)
Installer () . Windows- PHP. .
29 1. PHP: , ?
. 1.3. , PHP Windows
, - , . : , - . , : ! - ! ( ) ( ). 1.0, - . , 2.2 PHP 1.1 , , - . , - PHP, , -. .
30 1 . PHP MySQL
, http://windows .php .net/download, : . URL-, www.php.net. , Google PHP www.php.net, , Windows- , PHP.
- . Windows Allow () Next () .
. C:\Program Files\PHP\ PHP. - (. 1.4). PHP - - (Do not setup a web server). - .
. 1.4. -
PHP Windows. : ?PHP , Windows, - 2000 . PHP : (Thread Safe). Mac OS X Unix/Linux- , - , Windows . Windows-
31 1. PHP: , ?
, , , PHP , .
, . PHP Windows - , PHP- Windows , . PHP- - , -, Windows, PHP-, . PHP - PHP, . , (Non Thread Safe). - , : 10 40 % .
, , - PHP , , . . - , ( , , ), , , , , , .
(. 1.5). - Windows PHP-
. 1.5.
32 1 . PHP MySQL
(Extras) . . Next () .
Install () - . ! - PHP.
PHP - cmd. PHP. php (. 1.6).
. 1.6. PHP
PHP . .
PHP Macintosh Macintosh PHP. Macintosh PHP. Macintosh- Teinal.
Teinal PHP. Applications ( - Shift+ +A) Utilities . 1.7. Utilities
33 1. PHP: , ?
Mac OS X. -.
. 1.7. Utilities , Mac OS X
Shift+ +A , . , , Shift+ +A .
, , Finder . , . Shift+ +A , - . , .
, , . , Applications Utilities ().
34 1 . PHP MySQL
Applications () Teinal. (. 1.8). Teinal -Teinal - - Macintosh . PHP .
. 1.8. Terminal
Terminal PHP- . Terminal, dock-.
Teinal. -Teinal. -. - (. 1.9). Teinal - -Teinal - - - -. . .
. 1.9. Terminal
35 1. PHP: , ?
PHP php Enter. - .
Control+C . which php. which - php. . 1.10. php /usr/bin. .
. 1.10. which
php !
PHP , PHP ( php, ) -. , - Apple Software Update, PHP. , PHP -, Terminal php version. :
Bretts-MacBook-Pro:~ bdm0509$ php version
PHP 5.3.4 (cli) (built: Dec 15 2010 12:15:07)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
, PHP, , 5.3.4. ( - PHP (PC) .)
PHP, - www.php.net PHP. , Macintosh-, , - , unzip tar, - , .
36 1 . PHP MySQL
, Mac Software Update , . - . Software Update ( ) , - Macintosh-. -, .
PHP . -PHP . - . - PHP- HTML CSS aaScit. . HTML HTML CSS CSS aaScit S PHP -CSS CSS aaScit S PHP - CSS aaScit S PHP -aaScit S PHP - S PHP -PHP - - PHP.
PHP . PHP. Windows . Macintosh TextEdit. -TextEdit. -. - . PHP .
PHP. Windows NShee PhED (www.nusphere.com/products/phped.htm) (. 1.11).
NShee $50 $100 - - PHP - -PHP - - - - HTML CSS PHP.
Macintosh BBEdit (www.barebones.com/products/bbedit/index.html) TextMate (www.macromates.com). Macintosh- PhED Windows: HTML CSS aaScit -HTML CSS aaScit - CSS aaScit -CSS aaScit - aaScit -aaScit - - . BBEdit . 1.12. $100. BBEdit
37 1. PHP: , ?
PHP. HTML PHP - .
. 1.12. BBEdit
TextMate . 1.13. BBEdit. - . - TP . TextMate $60.
. 1.11. NuSphere PhpED JavaScript, CSS HTML, PHP.
PHP
38 1 . PHP MySQL
. 1.13. TextMate
: , PhpED, BBEdit TextMate, -, , - . , , Windows Finder Mac-, telnet Terminal, FTP- . : .
, dock- Macintosh- - Windows. - - - PHP.
. . , , , - , . - , - .
. FTP, , , - .
, , , - - FTP telnet. - , , TextMate PhpED.
39 1. PHP: , ?
PHP -PHP - - . - TextEdit. PHP .
: - Windows TextEdit Macintosh. PHP . - PHP -PHP - - .
PHP, Eclipse PHP (www.eclipse.org/projects/project.php?id=tools.pdt). Eclipse IDE Java-. PHP, PHP-. Eclipse , , , . .
PHP . PHP-. :
. . PHP HTML aaScit.
, , TextEdit, . , . PHP- . , , . TextEdit FormatMake Plain Text ( ). , . , .
40 1 . PHP MySQL
PHP -PHP - -. ($) (< > HTML) (\). (. 1.14).
. 1.14.
.
1. PHP- .
. $name =. \ns STDIN . - : .
. sayHello.php. .php . - - PHP- .
Windows Macintosh, , , , TXT. PHP. Windows , sayHello.php, - sayHello.php.txt.
41 1. PHP: , ?
PHP-!
- , - . , - . TextEdit Mac-, Preferences (). Preferences , , . Format () , - , Plain Text ( ) (. 1.15). Windows , .
. 1.15. TextEdit Preferences () +,
? - - . - PHP. . Mac- Teinal dock-. Teinal .
42 1 . PHP MySQL
sayHello.php. dir ( Windows) ls ( Macintosh). :
php sayHello.php
php sayHello.php. . - Enter. . 1.16.
. 1.16.
C PHP -. h .
! PHP.
PHP? PHP - aaScit. PHP -aaScit. PHP -. PHP -PHP - - -. PHP- - PHP HTML- PHP-.
: HTML- HTML- HTML-:
43 1. PHP: , ?
PHP & MySQL: The Missing Manual 1.1
! . , PHP-! : :
, HTML, , , - www.missingmanuals.com/cds/phpmysqlmm. CSS , , . (, PHP), .
. CSS sayHello.php .
:
- sayHelloWeb.php PHP- . sayHelloWeb.php .
PHP- HTML- sayHelloWeb.php . PHP- -PHP- -- -
44 1 . PHP MySQL
. - . .
PHP- ( - HTML sayHello.php):
PHP & MySQL: The Missing Manual 1.1
, ! . PHP-.
sayHelloWeb.php .
HTML. sayHello.php - PHP . PHP - PHP- - HTML. -HTML. -. - HTML. - .
HTML- . .
html head. body HTML- sayHello.html.
h1 .
45 1. PHP: , ?
. HTML- sayHello.html. PHP- -PHP- -- -
- HTML- ( ) PHP-. - ( sayHello.html) - . HTML- $_REQUEST PHP .
PHP -PHP - - . . PHP -PHP - - . PHP $. $myHeight yHeight $_REQUEST _REUEST.
PHP- $, PHP- , $ . , $myHeight PHP- , .
. . $myHeight ( ) 68 ( 68 1) 68 . PHP . -PHP . - . - : .
sayHelloWeb.php sayHello.html PHP- $_REQUEST. PHP $_REQUEST name (). $_REQUEST['name'] - - name (). phoneNumber ( ) PHP $_REQUEST['phoneNumber'].
1 68 173 . . .
46 1 . PHP MySQL
, $_REQUEST, . , , PHP, $_REQUEST.
PHP- name () echo - PHP-. HTML- .
HTML, CSS PHP PHP- -PHP- -- - PHP . - - .
- (HTML CSS aaScit) -. - www.yellowtagmedia.com/sayHello.html. - HTML - .
PHP . PHP- - HTML CSS. .
, , (/). - HTML-. URL- yellowtagmedia.com/ - -.
CSS (css/). CSS. JavaScript (js/). aaScit. - scripts/ PHP- (scits) .
PHP (scripts/). PHP-. php/ phpScripts/ - - scripts/ - .
(ch01/, ch02/ . .). - PHP-. . sayHello.html sayHelloWeb.php ch01/sayHello.html ch01/scripts/sayHelloWeb.php.
47 1. PHP: , ?
, , , - , . -, CSS, JavaScript PHP- HTML PHP.
HTML- PHP- . phpMM.css - www.missingmanuals.com/cds/phpmysqlmm CSS .
- . 1.17. HTML- PHP- ch01/. phpMM.css css/ - -.
. 1.17. -
48 1 . PHP MySQL
HTML- CSS- HTML- -HTML- CSS- HTML- -- CSS- HTML- -CSS- HTML- -- HTML- -HTML- -- - PHP-. ch01/scripts/ sayHelloWeb.php. PHP . - - ch01/sayHello.html. sayHello.html HTML -HTML - - . 1.18.
. 1.18. HTML-, PHP-. HTML- PHP-
. sayHelloWeb.php. - - (. 1.19). - - . sayHelloWeb.php .
. aaScit. PHP- .
49 1. PHP: , ?
. 1.19.
. . HTML- PHP- -- - .
2 PHP:
PHP- -PHP- -- - HTML-. . PHP -PHP - - . PHP. ( ).
- -. - PHP - -PHP - - - - - -. PHP -.
- sayHelloWeb.php name - sayHello.html :
echo $_REQUEST['name'];
$_REQUEST PHP- -. - .
$_REQUEST ( Twitte URL- acebook ):
51 2. PHP:
PHP & MySQL: The Missing Manual 2.1
, : : : : URL- Facebook: Twitter:
, HTML, .
HTML HTML 1. - . , HTML.
52 1 . PHP MySQL
HTML- . form , , : . , , , , - . HTML, - CSS.
. label for. , , , - , . fieldset, : - , . - , . HTML , ( HTML) - , : (labels), , , -. fieldset. CSS, . - - , CSS - . - HTML, , , 5 . . , , , CSS . , CSS HTML-, . 2.1.
, HTML CSS . - , . , , .
socialEntryForm.html. HTML ch02/. CSS css/ - - HTML-. . 2.1.
sayHelloWeb.php - $_REQUEST name. -.
53 2. PHP:
. 2.1. ,
- getFormInfo.php :
PHP & MySQL: The Missing Manual 2.1
, : : : : URL- Facebook: Twitter:
54 1 . PHP MySQL
, getFormIn-getFormIn-fo.php, - -. , socialEntryForm.html action, .
. first_name last_name name sayHelloWeb.php $_REQUEST .
- . getFormInfo.php (. 2.2).
. 2.2. PHP , - HTML, PHP-
$_REQUEST PHP-:
echo $_REQUEST['FORM_INPUT_FIELD_NAME'];
. - sayHello.php ( ).
:
$name = trim(fgets(STDIN));
55 2. PHP:
PHP . ( . ? !) :
$numberSix = 6;$thisIsMyName = "Brett";$carMake = "Honda";
getFormInfo.php. echo . .
PHP- . PHP HTML PHP HTML. PHP- .
PHP- html head head body. .
, - , , . - HTML PHP, HTML. , HTML- . ( , . .), , PHP HTML, , , getFormInfo.php, . PHP HTML, , , 20 30 PHP-, HTML.
56 1 . PHP MySQL
- . 2.2 . HTML- .
. $_REQUEST PHP- -PHP- -- - HTML -HTML - - HTML. -.
. - $first_name $last_name . . HTML- getFormInfo.php echo $_REQUEST . div- - "content":
, : : : : URL- Facebook: Twitter:
,
? !- PHP . PHP-, :
$x = $_REQUEST['username'];$y = $_REQUEST['password'];
, , - :
$username = $_REQUEST['username'];$password = $_REQUEST['password'];
, - . .
, , , , , - - ? , - :
echo " , " . $y;
, - , - $x, $y. : $x $y?
57 2. PHP:
, , !
, , , , - , .
socialEntryForm.html - . ( . 2.2). - -. . .
,
, , -, -. PHP- getFormInfo.php , - echo HTML, .
- , . - , , - . , , . , , . , -, . ( .)
, - , , . , - , , PHP-.
: . - , . , (
58 1 . PHP MySQL
), -.
getFormInfo.php. PHP, , HTML. ? , , . , , .
? - : $_REQUEST. - . ? PHP .
PHP PHP : . - . PHP cateilla - !(gUHa8@.@. Bet1. PHP . PHP -PHP . PHP - . PHP -PHP - - : (string). - .
PHP . getFormInfo.php :
$first_name = $_REQUEST['first_name'];$last_name = $_REQUEST['last_name'];$email = $_REQUEST['email'];
1 Google Bet .
59 2. PHP:
$facebook_url = $_REQUEST['facebook_url'];$twitter_handle = $_REQUEST['twitter_handle'];
: $first_name $last_name. . : ! - .
. - . y gil ygil.
PHP (.). getFormInfo.php HTML- :
: :
:
:
socialEntryForm.html - - . . 2.3.
. 2.3. , , -
60 1 . PHP MySQL
! : -. .
PHP . : " ". PHP - . - $first_name $last_name:
:
- . . 2.4 .
. 2.4. PHP , : $_REQUEST, , , ;
. PHP . PHP . - $facebook_url. :
: :
61 2. PHP:
62 1 . PHP MySQL
strpos() ( sting osition ( )) -sting osition ( )) - osition ( )) -osition ( )) - ( )) - - . $position 5 facebook.com $facebook_url 5. ( 5 6 .)
. - . $facebook_url ( $facebook_url facebook.com) $facebook_url facebook.com. $position ( false). strpos() $facebook_url .
strpos(), PHP, : , , , false.
$first_name = $_REQUEST['first_name'];$last_name = $_REQUEST['last_name'];$email = $_REQUEST['email'];$facebook_url = $_REQUEST['facebook_url'];$position = strpos($facebook_url, "facebook.com");if ($position === false) { $facebook_url = "http://www.facebook.com/" . $facebook_url;}$twitter_handle = $_REQUEST['twitter_handle'];
. .
1. strpos() - $facebook_url facebook.com. strpos() $position.
2. $position PHP- false if. if : $position false - { }.
3. { } true. $position === false. $facebook_url http://www.facebook.com acebook.
4. : $position false . { } .
63 2. PHP:
, PHP, Java, C Perl, 0. ( PHP ) , . , -, Thats weird, - T 0, 1.
. , - URL- Facebook getFormInfo.php : facebook.com/michael.greenfield. , , URL:
if (strpos($facebook_url, "facebook.com") > 0) { $facebook_url = "http://www.facebook.com/" . $facebook_url;}
PHP- : facebook.com - $facebook_url, - http://www.facebook.com/ $facebook_url.
, $facebook_url , : http://www.facebook.com/facebook.com/michael.greenfield. ?
, PHP 0, 1. 0 $facebook_url. f. 1 a, 2 c . . -, $facebook_url facebook.com, , . strpos() 0, , $facebook_url.
( , - , ), , . , , , 1 , 0 . , .
- socialEntryForm.html. acebook -acebook - - URL- facebook.com profile.php?id=100000039185327. .
. - PHP- . 2.5.
64 1 . PHP MySQL
( . 2.6) . profile.php?id=100000039185327 - URL- http://www.facebook.com/profile.php?id=100000039185327 (. 2.7).
. 2.6. -
. 2.7. URL- ,
65 2. PHP:
? . .
Twitte -. -Twitte -. - -. - Twitte @ : @bdmclaughlin. www.twitter.com - @ . Twitte @phpGuy URL- Twitte http://www.twitter.com/phpGuy.
Twitte .
1. $twitter_url http://www.twitter.com/.
2. Twitte @.
3. @ $twitter_handle - $twitter_url.
4. @ $twitter_handle @ $twitter_url.
5. Twitte HTML- .
- .
URL- Twitte :
$twitter_handle = $_REQUEST['twitter_handle'];$twitter_url = "http://www.twitter.com/";
- Twitte -Twitte - - $twitter_handle @. - strpos():
$twitter_handle = $_REQUEST['twitter_handle'];$twitter_url = "http://www.twitter.com/";$position = strpos($twitter_handle, "@");
- - @ $twitter_handle. if else:
$twitter_handle = $_REQUEST['twitter_handle'];$twitter_url = "http://www.twitter.com/";
66 1 . PHP MySQL
$position = strpos($twitter_handle, "@");if ($position === false) { $twitter_url = $twitter_url . $twitter_handle;} else { // @ Twitter}
. @ $twitter_url. @ .
strpos() - . PHP : substr(). substr() sbsting (-sbsting (- (-) . substr() .
substr("Hello", 2) "llo". "H" 0 "e" 1 "l" 2. substr() - 2 : "llo".
, PHP-, substr() strpos(), . , , . .
Twitte substr() - . @ - $position. - substr() $position $position + 1.
? PHP HTML PHP HTML, . , getFormInfo.php:
67 2. PHP:
, PHP HTML. , . PHP- . ( , - PHP, ?> , , .) , PHP - - , , , .
, PHP , a . , PHP- .
PHP , . - PHP-, , :
68 1 . PHP MySQL
HTML:
: :
69 2. PHP:
com org URL- http://www.facebook.com/profile.php?id=534643138 http://www.facebook.org/profile.php?id=534643138.
, com org. , - _.com, _.org _.org _.com - - .
PHP -PHP - - PHP- .
trim()PHP- trim() PHP . I loe y sace ba. I loe y sace ba..
PHP rtrim(), , ( ), ltrim(), ( ).
- - :
$first_name = trim($_REQUEST['first_name']);$last_name = trim($_REQUEST['last_name']);$email = trim($_REQUEST['email']);$facebook_url = trim($_REQUEST['facebook_url']);$position = strpos($facebook_url, "facebook.com");if ($position === false) { $facebook_url = "http://www.facebook.com/" . $facebook_url;}
$twitter_handle = trim($_REQUEST['twitter_handle']);$twitter_url = "http://www.twitter.com/";$position = strpos($twitter_handle, "@");if ($position === false) { $twitter_url = $twitter_url . $twitter_handle;} else { $twitter_url = $twitter_url . substr($twitter_handle, $position + 1);}
: $_REQUEST trim(). .
70 1 . PHP MySQL
trim() (, rtrim() ltrim()) , . trim() , , . , , , .
str_replace()
. str_replace() .
. facebook.org "facebook.org".
. facebook.org facebook.com "facebook.com".
. -.
( !) , , PHP . PHP-:
$facebook_url = str_replace( "facebook.org", "facebook.com", trim($_REQUEST['facebook_url']));
. , :
$facebook_url = $_REQUEST['facebook_url'];$facebook_url = trim($facebook_url);$facebook_url = str_replace( "facebook.org", "facebook.com", $facebook_url);
, - . , . ?
. - , - : - , , ? ,
71 2. PHP:
. , . , . , - , - . , , .
, - - . . , , . , ( ) - .
, , . , getFormInfo.php - , . 6, 7 10 , .
:
$facebook_url = str_replace("facebook.org", "facebook.com", trim($_REQUEST['facebook_url']));$position = strpos($facebook_url, "facebook.com");if ($position === false) { $facebook_url = "http://www.facebook.com/" . $facebook_url;}
, str_replace() , .
-. - PHP--PHP---- URL- facebook.org - . 2.9.
. 2.10 - getFormInfo.php . URL-.
View Source ( ). . -
72 1 . PHP MySQL
. . - (. 2.11).
. 2.9.
. 2.10.
73 2. PHP:
. 2.11. View Source ( )
$_REQUESTPHP . PHP-. -PHP-. --. - - .
. - . - : - .
PHP , PHP - . www.php.net/manual/en/ref.strings.php, , PHP.
. : - . - PHP www.php.net/manual , - . , , PHP . . (-, , , PHP , . .)
74 1 . PHP MySQL
-, PHP, PHP. , , , . . - PHP, , .
. - . . .
$file_cabinet . $file_cabinet URL- . . PHP :
- - .
, , - ( PHP) php. .
$file_cabinet :
$first_name = $file_cabinet[0];$last_name = $file_cabinet[1];$email = $file_cabinet[2];$facebook_url = $file_cabinet[3];$twitter_handle = $file_cabinet[4];
75 2. PHP:
, , , PHP (. . ). . $file_cabinet $file_cabinet[0], $file_cabinet[1].
. . - :
. - ? . - 2 4 URL- acebook? .
PHP . PHP . . .
. :
76 1 . PHP MySQL
$file_cabinet['facebook_url'] = "http://www.facebook.com/DerekTrucks";$file_cabinet['twitter_handle'] = "@derekandsusan";
$first_name = $file_cabinet['first_name'];$last_name = $file_cabinet['last_name'];$email = $file_cabinet['email'];$facebook_url = $file_cabinet['facebook_url'];$twitter_handle = $file_cabinet['twitter_handle'];
echo $first_name . " " . $last_name;echo "\n : " . $email;echo "\nURL- Facebook: " . $facebook_url;echo "\n Twitter: " . $twitter_url;
?>
$file_cabinet . ...
$_REQUEST PHP - $_REQUEST. . $_REQUEST['first_name'] .
( -) . PHP-.
. $file_cabinet['first_name'] $file_cabinet[0]. - $_REQUEST . $_REQUEST[0] PHP .
$_REQUEST? :
PHP & MySQL: The Missing Manual 2.2
77 2. PHP:
, $_REQUEST:
. foreach ( ). PHP:
foreach($_REQUEST as $value) {
foreach PHP . foreach $_REQUEST - . $value. - $value foreach. foreach $value .
if { } PHP :
foreach($_REQUEST as $value) { echo "" . $value . "";}
{ } -. $_REQUEST :
echo "" . $value . "";
: $value HTML-. $value $_REQUEST $_REQUEST.
$_REQUEST Derek Trucks @DerekAndSusan. PHP :
echo "" . "Derek" . "";echo "" . "Trucks" . "";echo "" . "@DerekAndSusan" . "";
78 1 . PHP MySQL
showRequestInfo.php. - socialEntryForm.php :
: : : URL- Facebook: Twitter:
socialEntryForm.html - -, socialEntryForm-2.html enterInformation.html. : , showRequestInfo.php, , getFormInfo.php.
- . -: showRequestInfo.php. - - (. 2.12).
? . . .
first_name last_name email . . PHP -PHP - - . - :
$value = $file_cabinet[$key];
$key. $key first_name :
$value = $file_cabinet['first_name'];
79 2. PHP:
. 2.12. -:
showRequestInfo.php $_REQUEST. - . PHP foreach -:
, $_REQUEST:
80 1 . PHP MySQL
. 2.13. PHP
, $_REQUEST: __utmz __utmc, - -. HTTP-, . .
? - . - socialEntryForm.html - ( !) . -. getFormInfo.php: -! -.
PHP- -PHP- -- - . PHP- -PHP- -- - . PHP- PHP- : -- : -. .
81 2. PHP:
: -? - ? . -. - .
. . : ! - . : .
, - . - . , - .
. ? . - .
3 MySQL SQL: : ? ? ? ? ? - : ?
- - PHP : . - . . PHP- .
? . . .
PHP (. -PHP (. - (. - $_REUEST 2). ? . - PHP . . .
83 3. MySQL SQL:
. - -. - - ! ? .
. . - . ( .)
, . : - , , , . - : , , .
- , . -, , . , , -, , - .
-. , - ( ).
, , -. . -, , - .
, - . , - , - .
84 1 . PHP MySQL
- : . . . - .
. . PHP ? - . ? . .
PHP , 4.
. - - ? . - .
. ? - - - .
-. Mac OS X Sotlight (. 3.1) - ickSile (http://quicksilver.en.softonic.com/mac). Sotlight Mac OS X -Sotlight Mac OS X - Mac OS X -Mac OS X - OS X -OS X - X -X - - . Sotlight .
Windows Deskto Seach Google (www.google.com/quicksearchbox) (. 3.2). Google Deskto Seach Windows Macintosh. Google Docents Gail. . .
85 3. MySQL SQL:
. 3.1. Spotlight
. 3.2. Google Desktop Search
: . -
86 1 . PHP MySQL
Sotlight Google Seach - Google Seach -Google Seach - Seach -Seach - - - . -.
()
- . -. . - .
. - . -. - . .
. . MySL ( - elation) : . (- PHP . .)
- . . .
MySQL . MySL -. - .
87 3. MySQL SQL:
, . , Oracle. , , MySQL: , , MySQL.
. , , MySQL, . , , Oracle ( IBM, PostgreSQL - ), PHP , MySQL, .
- - , , : . RDBMS (Relational Database Management Systems, ), RDBMS-. . RDBMS, , .
. - (Object-Oriented Database Management Systems, OODBMS). OODBMS 70- , 10 .
RDBMS , . , , . , RDBMS, , PHP- . , , , $_REQUEST['first_name'] Users, - first_name. , .
OODBMS , . User , $_REQUEST['first_name']. , , OODBMS User . , , , , .
, OODBMS , RDBMS, OODBMS . RDBMS, MySQL, -, OODBMS, .
88 1 . PHP MySQL
MySQL Windows MySL Windows . : Windows : 32- 64-. . - (. 3.3).
. 3.3.
Macintosh, .
. 32- 64- . -. . 3.3 32- Windows 7 Poessional. -Windows 7 Poessional. - 7 Poessional. -Poessional. -. - Windows . MySL 32- 64- .
- mysql.com (. 3.4). MySL
89 3. MySQL SQL:
. MySL . - MySL . - mysql.com: -.
. 3.4. , MySQL
Downloads (GA) (). MySL. MySL Conity See Download () . Windows (. 3.5). MSI Installe . Zi achie . MSI- : 32-bit 64-bit. - - MySL. . - MySL .
MySL. (. 3.6) - - .
90 1 . PHP MySQL
. 3.5. MySQL
. 3.6. MySQL
mysql-5.5.13-win32.exe. . - . Typical ().
- .
91 3. MySQL SQL:
MySL See Instance Conigation Wiad. MySL . -. MySL . -MySL . - . - - . : . MySL .
(Standard Configuration) (. 3.7). MySL Windows Windows MySL . Launch the MySQL Server automatically (- MySL-) MySL . MySL- bin Windows (Include Bin Directory in Windows PATH) (. 3.8). MySL . MySL -MySL - - . bin MySL (PATH).
. 3.7.
(Root Password) . www.amazon.com www.zappos.com 22-
92 1 . PHP MySQL
. MySL - myqsl_root. MySL . Execute () .
, , , , . - , , , , - , .
MySL -MySL - -. - MySL Coand Line Client (. 3.9). MySL Coand Line Client mysql. MySL- bin Windows- PATH - MySL (. . 3.8).
MySL Coand Line Client . -. : MySL. MySL PHP .
. 3.8. MySQL
93 3. MySQL SQL:
. 3.9. MySQL Command Line Client
. 3.10.
. 3.10. MySQL Command Line Client
94 1 . PHP MySQL
MySL -.
MySQL Mac OS X MySL Mac OS X Windows. -MySL Mac OS X Windows. - Mac OS X Windows. -Mac OS X Windows. - OS X Windows. -OS X Windows. - X Windows. -X Windows. - Windows. -Windows. -. - - www.mysql.com Downloads (GA) () - . MySQL Community Server. - Mac OS X .
Windows, MySQL Windows.
Windows, MySQL Macintosh . , MySQL, Compressed TAR Archive, MySQL. MySQL, .
DMG. MySL . : 32- 64-. - .
About This Mac ( Mac) More Info ( ) - . 3.11. Processor Name ( ).
. 3.11.
95 3. MySQL SQL:
Macintosh (32 64 ) . - 32- 64-. . Macintosh 32- 64-. . 3.1 - .
3.1. Macintosh
Intel Core Solo 32Intel Core Duo 32Intel Core 2 Duo 64Intel Quad-Core Xeon 64Dual-Core Intel Xeon 64Quad-Core Intel Xeon 64Core i3 64Core i5 64Core i7 64
Apple Macintosh. . 3.1, - http://support.apple.com/kb/HT3696. .
DMG- . ( ) .
DMG- . (. 3.12). DMG- . MySL -MySL - - ( ) - ReadMe.txt.
. 3.12. DMG- MySQL
96 1 . PHP MySQL
mysql-5.5.13-osx10.6-x86_64.pkg. . . .
, , , , . Macintosh, , . , , PHP MySQL.
MySL . Macintosh -Macintosh - - . (. 3.13). . - DMG (. . 3.12). -DMG (. . 3.12). - (. . 3.12). - .
. 3.13. MySQL
(Peeences Pane) -Peeences Pane) - Pane) -Pane) -) - (. 3.14).
97 3. MySQL SQL:
. .
. 3.14. MySQL Mac OS X
MySL (Automatically Start MySQL Server on Startup) . MySL .
Macintosh-. ( Applications UtilitiesTerminal (); - Terminal ). Terminal :
$ /usr/local/mysql/bin/mysql
. MySL. ( MySL - . mysql; . .)
MySL (. 3.15). Macintosh - . MySL PHP -MySL PHP - PHP -PHP - - MySL .
98 1 . PHP MySQL
. 3.15. MySQL Macintosh
, MySQL , MySQL Macintosh - , Preferences Pane, mysql Terminal. , - .
Terminal PATH. ( , PHP), , , . mysql, PATH - /usr/local/mysql/bin, , mysql . !
, PATH ? PATH, , -. Terminal :
$ defaults write com.apple.finder AppleShowAllFiles TRUE$ killall Finder
Finder, Macintosh, , , -. Finder, . Finder . (. 3.16). , , .
99 3. MySQL SQL:
, . , , , . , git, , .gitconfig, DropBox, , .dropbox.
. 3.16.
, .profile. - , TextEdit, Mac OS X. PATH, , . TextEdit .
:
MYSQL_HOME=/usr/local/mysqlexport PATH=$MYSQL_HOME/bin:$PATH
, . .profile, .
MYSQL_HOME, - , MySQL. , - MySQL, -, , $facebook_url PHP-
100 1 . PHP MySQL
(. str_replace() - 2). PATH -, bin, MYSQL_HOME. export Mac OS X PATH .
. , , (.), , . ( -, Finder.)
, .profile. , . Terminal mysql. - MySQL.
, - , Finder :
$ defaults write com.apple.finder AppleShowAllFiles TRUE$ killall Finder
, .
. 3.15 MySL .
SQL- MySL . Mac OS X Peeences Pane ( . 3.14) Windows - . - MySQL (. 3.17).
-, MySQL, , , .
MySL :
show databases;
, . MySQL PHP.
101 3. MySQL SQL:
. 3.17. MySQL
MySL -MySL - - :
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || development || eiat_testbed || mysql || nagios || ops_dashboard || performance_schema || test |+--------------------+8 rows in set (0.25 sec)
. : MySL -MySL - - .
show? : - databases. MySL .
102 1 . PHP MySQL
- : MySL . show databases; 8 . - 8 . - MySL.
MySL mysql MySL. -MySL. -. - use:
use mysql;
mysql. MySL mysql.
MySL -MySL - - :show tables;
:
mysql> show tables;+---------------------------+| Tables_in_mysql |+---------------------------+| columns_priv || db || event || func || general_log || help_category || help_keyword || help_relation || help_topic || host || ndb_binlog_index || plugin || proc || procs_priv || proxies_priv || servers || slow_log || tables_priv || time_zone || time_zone_leap_second || time_zone_name || time_zone_transition || time_zone_transition_type || user |+---------------------------+24 rows in set (0.00 sec)
MySL.
103 3. MySQL SQL:
mysql.
- . user. MySL :
mysql> select * from user;
select : . (*) : . from MySL : user .
. - - (. 3.18). MySL PHP .
. 3.18. select
104 1 . PHP MySQL
MySL. -MySL. -. - MySL PHP -MySL PHP - PHP -PHP - - .
. MySL user . .
. * - :
mysql> select Host, User, Password from user;
:
mysql> select Host, User, Password from user;+------------------------+-------+-------------------------------------------+| Host | User | Password |+------------------------+-------+-------------------------------------------+| localhost | root | *62425DC34224DAABF6995B46CDCC63D92B03D7E9|+------------------------+-------+-------------------------------------------+1 row in set (0.00 sec)
(localhost) - root. - MySL . - .
? -. first_name last_name.
. PHP- , MySQL . MySQL .
MySL -MySL - - - - .
105 3. MySQL SQL:
SQL
MySL SL (Stcted ey Langage). SL-:
mysql> select * from user;...mysql> select Host, User, Password from user;...
SL- SL. -SL- SL. -- SL. -SL. -. - SL . . SL -SL - - . :
mysql> select User, Password from users where first_name = 'Dirk' and country = 'Germany';
where - : users User Password (first_name) Dirk (country) Germany.
MySQL ? , PHP, -. , jino.ru, , , , - , -. - - .
( ), PHP MySQL ? , PHP-, , PHP ( MySQL) . telnet ssh, , - .
106 1 . PHP MySQL
, -.
-, , . , - , MacBook Pro. , PHP MySQL, , .
-, , , - ( ). , , . , . , , , . , .
, -, , . , , . - - , , , Windows, MySQL , Mac OS X, . , .
, , -, -. , , -. - , .
SQL , . - , - --, . ( ), , .
SL - . PHP-.
107 3. MySQL SQL:
- MySL -MySL - - -. - telnet ssh.
telnet ssh, Google , . , . - ssh, . - , .
MySL mysql. - PHP MySL mysql.
- :
bmclaugh@akila:~$ mysqlERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
MySL . - : - MySL http://mysql.kattare.com. - MySL -.
mysql , mysql hostname=localhost. MySQL localhost, , . MySQL, . , mysql .
MySL -MySL - -. mysql - . --hostname= - mysql MySL
108 1 . PHP MySQL
--user= mysql -.
MySQL, , admin root. , telnet ssh. , , , -. : , , -.
:
bmclaugh@akila:~$ mysql --host=dc2-mysql-02.kattare.com --user=bmclaugh --passwordEnter password:
--password MySL . --password=__ MySL-.
MySL . 3.19.
- SL.
. 3.19. MySQL
109 3. MySQL SQL:
USE
MySL - MySL . SL-:
myqsl> show databases;
. :
myqsl> show databases;
+----------+| Database |+----------+| bmclaugh |+----------+1 row in set (0.09 sec)
-. - mysql user. - . lbe ljuber db-ljuber - .
. MySL -MySL - -:
mysql> use bmclaugh;Database changed
MySQL . , MySQL- use [___].
MySL. SL- . - :
mysql> SELECT * FROM user;...mysql> SELECT Host, User, Password FROM user;...
110 1 . PHP MySQL
mysql> SELECT User, Password FROM users WHERE first_name = 'Dirk' AND country = 'Germany';
SELECT FROM WHERE AND - . MySL .
MySQL, SELECT WHERE, , - . .
CREATE
mysql USE - SELECT users. . - SELECT - .
SL CREATE. . - .
MySL :
CREATE TABLE users (
. Enter :
mysql> CREATE TABLE users ( ->
? MySL . . MySL -MySL - -: . SL - Enter. MySL
111 3. MySQL SQL:
- . (->) - MySL .
:
mysql> CREATE TABLE users ( -> user_id int, -> first_name varchar(20), -> last_name varchar(30), -> email varchar(50), -> facebook_url varchar(100), -> twitter_handle varchar(20) -> );
Enter - :
mysql> CREATE TABLE users ( -> user_id int, -> first_name varchar(20), -> last_name varchar(30), -> email varchar(50), -> facebook_url varchar(100), -> twitter_handle varchar(20) -> );Query OK, 0 rows affected (0.18 sec)
: . CREATE.
CREATE MySL -.
TABLE MySL . .
users . ( MySL .
user_id int varchar(20).
) MySL .
- . int intege ( ) 1 890 239 402 - int 1293 31456 .
112 1 . PHP MySQL
MySQL int, integer. MySQL .
varchar . varchar (aiable chaacte) () . varchar(20) 1 20 .
MySL -MySL - - int (user_id) - varchar .
CREATE ? SHOW:
mysql> SHOW tables;+------------------------------------+| Tables_in_bmclaugh |+------------------------------------+| users |+------------------------------------+1 row in set (0.06 sec)
? PHP MySQL . , , MySQL . MySQL :
mysql> useERROR:USE must be followed by a database namemysql>
. - , , Enter:
mysql> SELECT *, -> FROM -> ->
SELECT * . MySQL Enter ->.
, MySQL SQL. , , .
113 3. MySQL SQL:
, (;), Enter. , - , MySQL . , , . .
. ? : DESCRIBE (). users:
mysql> DESCRIBE users;+----------------+--------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------------+--------------+------+-----+---------+-------+| user_id | int(11) | YES | | NULL | || first_name | varchar(20) | YES | | NULL | || last_name | varchar(30) | YES | | NULL | || email | varchar(50) | YES | | NULL | || facebook_url | varchar(100) | YES | | NULL | || twitter_handle | varchar(20) | YES | | NULL | |+----------------+--------------+------+-----+---------+-------+6 rows in set (0.04 sec)
DESCRIBE DESC ( desc). DESCRIBE users; SQL.
MySL : users . .
, , , varchar. , 10 20 varchar(100), - .
. , . , - . , .
114 1 . PHP MySQL
, - , , URL- Facebook. 15 ( -!). , www.facebook.com 20 . .
. - , , - , varchar . , , -, .
, . varchar , , . , .
DROP
MySL SL -MySL SL - SL -SL - - . . delete () DROP.
users CREATE - users SL-:
mysql> DROP TABLE users;Query OK, 0 rows affected (0.10 sec)
!
mysql> SHOW tables;+------------------------------------+| Tables_in_bmclaugh |+------------------------------------+0 rows in set (0.06 sec)
. ... SELECT. . MySL -MySL - - CREATE users.
115 3. MySQL SQL:
. , - . - .
INSERT
users. . - INSERT .
:
mysql> INSERT INTO users -> VALUES (1, "Mike", "Greenfield", "[email protected]", -> "http://www.facebook.com/profile.php?id=699186223", -> "@greenfieldguitars");Query OK, 1 row affected (0.00 sec)
! SL . users -.
-. - DESCRIBE:
mysql> DESCRIBE users;+----------------+--------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------------+--------------+------+-----+---------+-------+| user_id | int(11) | YES | | NULL | || first_name | varchar(20) | YES | | NULL | || last_name | varchar(30) | YES | | NULL | || email | varchar(50) | YES | | NULL | || facebook_url | varchar(100) | YES | | NULL | || twitter_handle | varchar(20) | YES | | NULL | |+----------------+--------------+------+-----+---------+-------+6 rows in set (0.29 sec)
1 user_id; "Mike" first_name . .
: . - INSERT INSERT PHP-.
116 1 . PHP MySQL
SELECT - SELECT. - SELECT * FROM user DROP CREATE INSERT . :
mysql> SELECT * FROM users;+---------+------------+------------+----------------------------+--------------------------------------------------+--------------------+| user_Id | first_name | last_name | email |facebook_url | twitter_handle |+---------+------------+------------+----------------------------+--------------------------------------------------+--------------------+| 1 | Mike | Greenfield | [email protected] |http://www.facebook.com/profile.php?id=699186223 | @greenfieldguitars|+---------+------------+------------+----------------------------+--------------------------------------------------+--------------------+1 row in set (0.00 sec)
- . ( - SL- MySL) . .
. :
mysql> SELECT first_name, last_name, twitter_handle FROM users;+------------+------------+--------------------+| first_name | last_name | twitter_handle |+------------+------------+--------------------+| Mike | Greenfield | @greenfieldguitars |+------------+------------+--------------------+1 row in set (0.00 sec)
. MySL -MySL - - PHP . PHP - . - .
SELECT. WHERE:
117 3. MySQL SQL:
mysql> SELECT facebook_url -> FROM users -> WHERE first_name = 'Mike';+--------------------------------------------------+| facebook_url |+--------------------------------------------------+| http://www.facebook.com/profile.php?id=699186223 |+--------------------------------------------------+1 row in set (0.00 sec)
WHERE -. - - SL.
SQL MySQL? , SQL MySQL. , SQL MySQL. - , , SQL MySQL.
SQL . MySQL - , PostgreSQL Oracle.
, SQL . , SQL - , . , SQL , .
MySQL . , SQL. , SQL, SQL. MySQL SQL ( ).
SQL MySQL, -. , PHP MySQL, SQL. , SQL- , SQL. - SQL , - MySQL. , PostgreSQL Oracle, .
118 1 . PHP MySQL
, , SQL . SQL. - . ( , .) .
, SQL, . - .
SQL, MySQL , , SQL.
2
-
4. PHP MySQL
5.
6. -
4 PHP MySQL PHP MySL -PHP MySL - MySL -MySL - - . . PHP MySL php.
PHP . . - SL- MySL. PHP -. PHP -PHP - - mysql.
PHP MySL . .
PHP-,
.
1. MySL.
2. USE MySL.
3. SL .
4. .
5. .
121 4. PHP MySQL
3 4 5 . .
MySL .
MySQL PHP- . PHP MySL. - :
bmclaugh@akila:~$ mysql --host=dc2-mysql-02.kattare.com --user=bmclaugh --password
PHP : .
connect.php. USE SL- .
:
, PHP , - , , localhost, .
! - .
: mysql_connect. . mysql - .
122 2 . -
.._, -_ - .
die? ( die ). : die - - . die : - . die - .
die mysql_connect. mysql_connect - . PHP- PHP MySL . mysql_connect - mysql_connect : false.
:
123 4. PHP MySQL
MySL! . - . ( die - . - .)
. mysql_connect . mysql_error. - mysql_error die .
mysql_connect, mysql_error die . , , . , , .
. PHP-. , , .
mysql_connect . PHP die :
echo " MySQL!";
HTML- connect.html.
HTML:
PHP & MySQL: The Missing Manual 4.1
SQL
124 2 . -
: - connect.php. (. 4.1) MySQL. connect.html . CSS. - ? . - CSS. .
. 4.1. MySQL
- : ! - . 4.2.
. 4.2. MySQL
125 4. PHP MySQL
PHP- . - : MySL ? PHP .
-: mysql_ : - . - - false ( null nil).
MySL -MySL - - PHP-. mysql_select_db.
mysql_ . - : www.php.net/manual/ru/ref.mysql.php. - , , .
- , die PHP-, . PHP , :
mysql_connect("___", "_", "");
, , , die.
: die - , , , , . , die , , , :
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
, , - , , die. , - - .
126 2 . -
, -, - , CSS- . die.
PHP , - , . , die , - . , - , , - . , die ( - ) , - .
, . die. , , , - 7. die .
mysql_select_db USE false. - connect.php:
. die - . .
127 4. PHP MySQL
. connect.html ( USE). . 4.3. : SL.
. 4.3. MySQL,
SHOW
. MySL . . HTML- -HTML- -- -.
- . connect.php :
PHP MySL: mysql_query. - SL . -SL . - . - SL SL-:
SHOW TABLES;
SL- .
128 2 . -
die? ? - . : mysql_query $result. . - SHOW TABLES - . $result false mysql_ false.
false :
PHP- . === PHP false. PHP - : !. $some-variable false : if (!$some-variable). ! : $some-variable false.
! not (). : i not $eslt then die ( $result true ). :
PHP .
129 4. PHP MySQL
PHP . , , , ? , ... , , ? , , . . , , . . PHP-, PHP, .
SL- :
connect.html (. 4.4). - - .
. 4.4.
SQL- - $result .
130 2 . -
. $result - PHP- - . PHP -PHP - - - PHP.
: mysql_query SL- SHOW TABLES. PHP MySL SL. $result : . - mysql_query. : - mysql_query $result URL- acebook CREATE TABLE.
PHP-. - ; PHP