Title: Web Database with PHP
1Web Databasewith PHP MySQL
Taweerat Nualchuay Computer Science
Department Songkhla Rajabhat University
2Objectives
- After completing this lesson, you should be to
do following - Write the php script embedded into HTML
- Using php to control MySQL database
3How to Web database Work
Web Server
Database
FreeBSD
4What is the PHP
- PHP Hypertext Preprocessor
- A widely-used Open Source general-purpose
scripting language. - Suitable for Web development / embedded into
HTML. - Server-Side scripting.
- Simple for a newcomer
5What can PHP do?
- Server-side scripting (CGI or server module)
- Command line scripting (Interpreter)
- Client-side GUI applications (PHP-GTK extension)
- Usable on all major operating systems and support
for most of the web servers - Support a wide range of databases
6Why PHP?
- Performance Issue ?
- The Zdnet did a evaluation and benchmarking of 4
web scripting languages. During benchmarking, the
same spec and identical cpu, memory boxes were
used. Under identical conditions, it was found
that PHP was the fastest - about 3.7 times faster
than JSP and about 1.2 times faster than ASP.
Read the report at eWeek and mirror-site The
benchmark results are - - PHP pumped out about 47 pages/second
- Microsoft ASP pumped out about 43 pages/second
- Allaire ColdFusion pumped out about 29
pages/second - Sun Java JSP pumped out about 13 pages/second
7Why PHP? (cont)
- Open source
- No cost implementation PHP
- Server side
- Open Source
- Crossable Platform
- HTML embedded
- Simple language
8PHP code
Embedding PHP in HTML lthtmlgt ltbodygt ltstronggtHell
o World!lt/stronggtltbr /gt lt? echo My name is
Taweerat ?gt lt/bodygt lt/htmlgt PHP tag styles
XML lt?php ?gt, ASP lt gt Script ltscript
languagephpgtlt/scriptgt
9Language reference
- Comments
- - ???????????? Comment ??????? C, C ??? Unix
- ????????
- lt?php
- echo Hello ! // ??? comment ??? 1 ??????
- / ?????????????
- ??????? 2 ???????????? /
- echo World ??? comment ??? shell-style
- ?gt
10?????? echo, ??????
- ??????????????????????????????????????? browser
- ???????????????
- echo ???????1 ??????????1, ???????2
??????????2, ???????3 ??????????3, - ??????? ?????????????????????? double quote ( )
???? single quote ( ) - ????????????? PHP ???????????????????????? ????
???????????? Perl
11??????????? 1
- ltHTMLgt
- ltHEADgt
- ltTITLEgtExample 1lt/TITLEgt
- ltBODYgt
- lt? echo "Hi, I'm a PHP script!" ?gt
- lt/BODYgt
- lt/HTMLgt
12??????????? 2
- ltHTMLgt
- ltHEADgt
- ltTITLEgt Example 2lt/TITLEgt
- lt/HEADgt
- ltBODYgt
- Todays Date lt? print(Date("l F d, Y")) ?gt
- lt/BODYgt
- lt/HTMLgt
13??????????? 3
- ltHTMLgt
- ltHEADgt
- ltTITLEgt Example 3lt/TITLEgt
- lt/HEADgt
- ltBODYgt
- Todays Date
- lt?
- /
- print todays date
- /
- print(Date("l F d, Y"))
- ?gt
- lt/BODYgt
- lt/HTMLgt
Today's Date Sunday August 29, 2004
14Data Types
- - Integer ?????????????????????? ???? 1, 10,
100, 123 ??????? - floating-point numbers ???????????????????
???? 1.10, 1.2e-10 ??????? - Single- quoted String ??????????????????????????
?????????? single quote - Double-quoted String ??? ????????????????????????
??????????????? double quote ????????
single-quoted string ?????????????????????????????
????????????????? \ ?????????? escape sequence
???? meta characters ?????????
15Variable
Variable
- ????????????????????????????????
- ??????????????????????????????????????
- PHP ?????????????????????????????? ? ????
- X A12 // ?????? X ???????? string
- Y 123 // ?????? Y ???????? string
- Z ABC // ?????? Z ???????? string
- I 123 // ?????? I ???????? integer
- J 1.23 // ?????? J ????????
floating-point - (?????? ???? ???????????????????? PHP
?????????????????? string ?????)
16Expressions
Operator
- ??? ??? assign ???????????????????????????
- ???????? b (a 5) 1 // ???????? a 5
??? b 6 - b a 5 1 // ???????? a ??? b
6 - ?? 2 ??????? pre-increment ??? post-increment
- pre-increment ??? x ?????? x ??????????? ?
?????????????????????????????????? assign
???????????? - ???? a 5
- b a
- // ???????? a ??? b 6
17Expressions (???)
- post-increment ??? x ?????? x ??????????? ?
?????????? assign ??????????????????????????????? - ????
- a 5
- b a
- // ???????? ??? a 6 ??? b 5
18String Operation
- PHP ?? operator ???????????????? string 1 ??????
"." - ??????????????????
- ??????????? a "Hi ! "ba . PHP"b
??????????? Hi ! PHP
19Operator Precedence
- PHP ??????????????????????????????????????????????
??????????????? - ????????
- 163 ??????????? 19 ?????? 21 ????? PHP
????????????????????????????? (??????) ???????
(??????) ????????????????????????
20??? Connect Database
- Filename function.inc.php
- lt?
- hostname "localhost"
- user ""
- password ""
- dbname ""
- link mysql_connect(hostname, user,
password) or die("die") - ?gt
21??????????? ???????? function.inc.php
- lthtmlgt
- ltbodygt
- lt?
- Include(function.inc.php)
- // other php script
- echo ltbrgt
- ?gt
- lt/bodygt
- lt/htmlgt
22??? Query Database
- lt? include("function.inc.php")
- sql"SELECT
- FROM user_login_table
- WHERE LOGIN_NAME 'user_login'
- AND PASSWORD 'pass_word'"
- sel mysql("dbname","sql")
- rowmysql_num_rows(sel)
- if (rowgt0)
- // ??????????? ???????????
- else
- // ?????????????? ???????????
-
- ?gt
23?????? session
- lt? session_start()
- session_register("USER_LOGIN")
- session_register(PASSWORD")
- USER_LOGIN user_login
- PASSWORD pass_word
- ?gt
- ????????? session ????????? Sign Out
- lt?
- session_start()
- session_destroy()
- echo "ltMETA HTTP-EQUIV\"REFRESH\" CONTENT\"0
URLindex.php\"gt" - ?gt
24?????? session ?????????? ?
- ????????????????????????? sign in ?????????
- lt? session_start()
- if(!session_is_registered("USER_LOGIN"))
- echo "ltmeta http-equiv\"refresh\"
content'0URLindex.php'gt" -
- ?gt
25??? Insert
- lt? include("function.inc.php")
- sql "INSERT INTO table
- VALUES('var1' , var2 , 'var3'"
- result mysql_db_query(dbname,sql)
- if(!result)
- echo " ??????????????? ???????????????"
- else
- echo "????????????"
- ?gt
26????????
- lt? include("function.inc.php")
- sql"SELECT ??????
- FROM ?????
- WHERE ???????
- ORDER BY ????"
- s_selmysql("dbname","sql")
- c_rowmysql_num_rows(s_sel)
- if (c_row gt0) ?gt
27????????? (?????)
- lt? for(start 0startltc_rowstart)
- dept_id mysql_result(s_sel,start,DEPT_ID)
- dept_namemysql_result(s_sel,start,DEPT_NAME)
echo dept_id - echo dept_name
-
- ?gt
28??? Update
- ???????????????
- ??????????????????
- ??????????????????????????????????????????,
?????? - ???????????????????????????????????????????????
- ????????? Update ??? SQL
29Update (cont)
- lt? Include(function.inc.php)
- sql "UPDATE table SET ?????
'?????', - ????? '??????
- result mysql_db_query(dbname,sql)
- if(!result)
- echo ?????????????????
- else
- echo ???????????
-
- ?gt
30????? (?????????????????????????????)
- lt? Include(function.inc.php)
- sql DELETE FROM table
- WHERE ????? ???????
- result mysql_db_query(dbname,sql)
- if(!result)
- echo ??????????????
- else
- echo ????????
-
- ?gt
31??? JavaScript ????????????
- ???????????????????????
- ???
- onClick "return confirm(' ???????????????????
Y/N ')"gt