Title: CH07 PHP
1CH07 PHPMySQL
2??Apache?php?Mysql??
3Apache??
- Apache???????,???????????IIS(Internet Information
Services) - Apache????
- ???(Linux?Windows?MAC OS X?)
- ????????
- ????(??????????)
4Php??
- PHP????????????,????????????,?????HTML??PHP???????
???,????????????????????,????,???PHP?? - ????PHP???
- (1)??????????
- (2)?????????
- (3)????,??????????????????Java?COM?XML?
- (4)???(?????Web???)
- (5)?????
- (6)?????????????
5PHP?Client Script(?VBscript)???
- ????Script
- ??????????,??????????????????????,?Javascript?VBsc
ript? - ????Script
- ??????????,???????,?????HTML,?????????????,???????
,?PHP?JSP?
6Mysql
- MySQL??????????,??MySQL AB????????????,????C?C?J
ava?Perl?PHP??,????????????,?Linux?Windows?Sun
Solaris ... ?,??????ODBC????????? - ???????MySQL?????,??????????,????????????????????,
???????
7??APPSEV??
8AppServ
- AppServ????Apache?PHP?Mysql???????
- ??????
9STEP 1
10STEP 2
11STEP 3
12STEP 4
??usernameroot?password(??)
13STEP 5
14??PHP????
15???PHP??HTML
??
lthtmlgt ltheadgt lttitlegtHellolt/titlegt lt/headgt ltbodygt
lth1gtPHP/MySQL??lt/h1gt lt?php echo "???? PHP
Script" ?gt lt/bodygt lt/htmlgt
PHP????
??Web Server ???????PHP???,??????,??? .php? .phtml
16PHP??
??
- PHP???????????PHP????PHP???????
- PHP??????????
- ????????PHP???,???
lt?php echo "This is a PHP Scriptltbrgt" echo "The
Best Choice!" ?gt
lt?php echo date("Y-m-d") ?gt
????
17PHP??
- ??????????????,????????????,PHP????????????,PHP??C
,C????? - (1)C???
- /
- ??????
- /
- (2)C???
- // ??????
- (3)Shell Script ??
- ??????
18PHP??
?? ??
(int) or (integer) ??
(real) or (double) or (float) ???
(string) ??
(array) ??
(object) ??
19PHP??
- ??????? . ????? ?total?vote_count .
?????? ?total?Total????????? .
???????????? ?2a -gt?? . ?????????????,????
?substr?array? . ????,??????,????????? - ????????
20PHP??(????)
??
??????????,???????? .
name "???" //?????? echo "?????ltbrgt"
echo "?????" .name a1 56 //?????? a2
42 //?????? a3 a1a2 //?????? echo
"ltbrgt ????nbspnbsp???" .a3
????
21????
?? ?? ??
???????????????????? a5
a a 5 a5
- a a - 5 a-5
a a 5 a5
/ a a / 5 a/5
22P7-1
23PHP??(????)
??
lt?php your_ip "??IP?" . _SERVER'REMOTE_ADDR
' . "ltbrgt" your_browser "??????" . _SERVER
'HTTP_USER_AGENT' ."ltbrgt" run_script "????
?????" . _SERVER'PHP_SELF' . "ltbrgt"
echo your_ip echo your_browser
echo run_script ?gt
24PHP??(????)
- ?????????
- _GET????
- ?????GET??????????
- _POST????
- ?????POST??????????
- ????????_FILES
?????
???????????
MIME????????
25PHP ???
- ?????????????,????echo(PHP?????)?
print(??)????,??echo??print????
26??HTML??
- ????
- ltFORMgt...lt/FORMgt ??
- ????
- ACCEPT-CHARSET...???????
- ACTIONURL??????????????
- ENCTYPE...???MIME(???Internet????)
- METHOD"GET,POST
- NAME"..."
- TARGET"..."
- .
27?? HTML??
- ???????
- ltINPUTgt ??
- ????
- TYPE"TEXT,PASSWORD,RADIO,CHECKBOX,
SUBMIT,RESET,FILE,IMAGE,HIDDEN,BUTTON" - ??????????????,?????TYPE??????????????
28?? HTML??
- ?????-????
- ???????TYPE"TEXT,???????
29?? HTML??
- ?????-????
- ltinput typeSubmit value??????"gt
- ?????-??????
- ltinput typeReset value??????"gt
30?? HTML??????
??
lthtmlgt ltheadgt lttitlegtPHP????lt/titlegt lt/headgt ltFORM
METHODPOST ACTION"test06_1.php"gt ? ltINPUT
TYPE"text" NAME"h"gtltBRgt ? ltINPUT TYPE"text"
NAME"w"gtltBRgt ltINPUT TYPE"submit"
value"??"gt lt/FORMgt ltbodygt lt/bodygt lt/htmlgt
31?????????(????????)
lthtmlgt ltheadgt lttitlegtUntitled Documentlt/titlegt ltme
ta http-equiv"Content-Type" content"text/html
charsetbig5"gt lt/headgt ltbodygt lt?php w
_POSTw h _POSTh area wh para
2 (_POSTw _POSTh) echo
ltltlttableprint lttable width"193"gt lttrgt lttd
width"52"gt?lt/tdgt lttd width"99"gt_POSThlt/tdgt lt
/trgt lttrgt lttdgt?lt/tdgt lttdgt_POSTwlt/tdgt lt/trgt ltt
rgt lttdgt??lt/tdgt lttdgtarealt/tdgt lt/trgt lttrgt lttdgt??
lt/tdgt lttdgtparalt/tdgt lt/trgt lt/tablegt tableprint ?
gt lt/bodygt lt/htmlgt
Test06_1.php
32??
- ???PHP??, ?????, ????, ????????? (??)
33????
- ??
- UPLOAD.HTML
- UPLOAD.PHP
34??MySQL??
35MySQL
- MySQL???????????????SQL????????SQL(???????)???????
???????????? - MySQL???????/????????,?????????????mysqld?????????
??????
36MySQL??
- ????????????????????????????CPU(???)?
- C ?C? Eiffel ? Java? Perl? PHP?Python?? TCL API
??20 MySQL ????? API? - ??????????,?Linux?Windows?MacOS ?
- ?????????????????,???????????????????,????????????
,?????????? - ??????16???????????116?????????????????? 256 ?byt
e(???MySQL?,?????)???????????CHAR?VARCHAR?????? - ??????????????SQL????????????????,????????????????
????
37MySQL????
- MySQL??????
- ????
- ???????
- ????
38MySQL????-????
??? ??????
TINYINT 1 byte
SMALLINT 2 ?byte
MEDIUMINT 3 ?byte
INT 4 ?byte
INTEGER 4 ?byte
BIGINT 8 ?byte
FLOAT(X) 4 ?? X lt 24 ? 8 ?? 25 lt X lt 53
FLOAT 4 ?byte
DOUBLE 8 ?byte
DOUBLE PRECISION 8 ?byte
REAL 8 ?byte
DECIMAL(M,D) Mbyte(D2 , ??M lt D)
NUMERIC(M,D) Mbyte(D2 , ??M lt D)
39MySQL????-???????
??? ??????
DATE 3 ?byte
DATETIME 8 ?byte
TIMESTAMP 4 ?byte
TIME 3 ?byte
YEAR 1 byte
40MySQL????-????
??? ??????
CHAR(M) Mbyte,1 lt M lt 255
VARCHAR(M) L1 byte, ??L lt M?1 lt M lt 255 (????,????????????1)
TINYBLOB, TINYTEXT L1 byte, ??Llt 2 8
BLOB, TEXT L2 byte, ??Llt 2 16
MEDIUMBLOB, MEDIUMTEXT L3 byte, ??Llt 2 24
LONGBLOB, LONGTEXT L4 byte, ??Llt 2 32
ENUM('value1','value2',...) 1 ? 2 ?byte, ?????????(???65535)
SET('value1','value2',...) 1,2,3,4?8?byte, ??????????(??64???)
41??MySQL?????
42MySQL?????
Mysql???????????
Mysql????????
43Mysql?????
44??mysql.exe??MySQL????
??MySQL???????
?????lin_DB ?slq??
??MySQL
45???????
46??mysql.exe??MySQL
- Step 1 ??????-???????
- Step 2???????,????
- cd\appserv\mysql
- Step 3??mysql???,????
- mysql u root p
- enter passwordtest
- Step 4??mysql????,??sql?????????????????
-U?????????? ? p ?????????
??
47??MySQL??????
- ?????board
- Create database board
- ?????message
??board???
?????message?????
48MySQL???????sql????
a.sql???c\appserv\mysql????
49MySQL???????sql????
50MYSQL???????
?? ??
tbl_name.frm ???(??)??
tbl_name.MYD ????
tbl_name.MYI ????
51???phpMyAdmin ?? ??MySQLy ???
52phpMyAdmin??(?)
- ??phpMyAdmin?????C\AppServ\www\phpMyAdmin
- ????(??????,?????)
- ???????(wordpad)????? config.inc.php??
-
- ....(?) If the auto-detection code does work
properly, you can set to TRUE the
cfg'PmaAbsoluteUri_DisableWarning' variable
below./cfg'PmaAbsoluteUri'
'http//192.168.0.xxx/phpMyAdmin/' (??
phpMyAdmin ?????)
53phpMyAdmin??(?)
- ????phpMyAdmin????
- 1. ??IE
- 2. ???http//localhost/phpMyadmin/main.php
- 3. ??????
- ???????phpmyadmin.doc????
54phpMyAdmin??(?)
???????
55??PHP MYSQL
56PHP??mysql??????
- ?????
- ?????
- ??SQL???? or ????
- ?????? ????????? SELECT???
- ???????
57?????
- ????mysql_connect("Mysql????", "??", "??")
- ???????,????? MySQL link ?????,??????? FALSE
- EX.
- //?root??MySQL???
- host"localhost"
- user"root"
- ps"test"
- link mysql_connect(host,user,ps)
58?????
- ?????mysql_select_db("?????")
- ??????,??? True ?,???? False ??
- Ex.
- //??board??????
- dbase"board"
- mysql_select_db(dbase,link)
????????
59??SQL??
- ??SQL??(??) query "SELECT FROM my_table"
(??) query "INSERT INTO my_table (c1,c2,...)
VALUES ( value1,value2, ...)"(??) query
"UPDATE my_table SET c1'abc' WHERE id'100'
"(??) query "DELETE FROM my_table WHERE
id'100' " - ??SQL????????result mysql_query(query)
60??SQL??
- EX1.??????
- //??SQL??,?????board???,????????
- insert_str "Insert into message(name,content,
time) - Values('uname','content',Now())"
- mysql_query(insert_str,link)
- EX2.????
- //??????
- //????????????????,????????
- select_str"Select From message Order by no
DESC" - resultmysql_query(select_str,link)
61?????
- //???????
- mysql_close(link)
62?????
63?????
64lt?php //??MySQL???? host"localhost" user"roo
t" ps"test" dbase"board" //???????????????
uname"" content"" //????????
if(isset(_POST"uname")) uname
_POST"uname" if(isset(_POST"content"))
content _POST"content"
//???????????????? if(!empty(uname)
!empty(content)) //?root??MySQL??? link
mysql_connect(host,user,ps)
//??board?????? mysql_select_db(dbase,link)
//???????,nl2b()???????????ltbrgt
//addslashes()?????????? \ contentaddslashes
(nl2br(content)) //??SQL??,?????board???,??
?????? insert_str "Insert into
message(name,content,time) Values('uname','con
tent',Now())" mysql_query(insert_str,link)
//??????? mysql_close(link) ?gt
mysql ???????
65lt?php host"localhost" user"root" ps"test"
dbase"board" //?board????,??????????????????
//?linda??MySQL??? link mysql_connect(host,
user,ps) //??surpro?????? mysql_select_db(db
ase,link) //?????? //????????????????,???????
? select_str"Select From message Order by
no DESC" resultmysql_query(select_str,link)
//?????????? rowsmysql_num_rows(result)
//????????? for(x0xltrowsx)
//??????????????? arrdataxmysql_fetch_array
(result,MYSQL_ASSOC) //???? echo
arrdatax'time'."--gtgt" echo
arrdatax'name'." ????ltpgt" echo
arrdatax'content'."lthrgt" //????
mysql_close(link) ?gt
mysql ???????