•Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page...

52
PHP Professional Home Page PHP Hypertext Processor Server Side Script www.php.net

Transcript of •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page...

Page 1: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

PHP

•Professional Home Page•PHP Hypertext Processor•Server Side Script•www.php.net

Page 2: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

จุดเดน PHP

• Free• Speed• Open Source• Crossable Platform• Database Access• Protocol Support• Library• Easy

Page 3: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

หลักการทํางานของ PHP

client Browser(HTML) Server

PHPInterpreter

PHPLibraries Other

Database

MySQL

ODBC

SQL Server

Access

ผลสงกลับ Browser

Server สงคําสั่ง PHP

สงผลเปน html กลับ Server

แสดงผลใหผูใช

เรียกโปรแกรมPHP ผาน Browser สงตอไป Server

Page 4: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

รปูแบบการเขียนสคริปต PHP• ทั่วไป (แบบภาษา SGML)

<?คําสั่งในภาษา PHP;?>

• ลักษณะภาษา XML<?phpคําสั่งในภาษา PHP;?>

Page 5: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

รูปแบบการเขียนสคริปต PHP (ตอ)• ลักษณะ Javascript

<Script Language=“php”>คําสั่งในภาษา PHP;</script>

• ลักษณะภาษา ASP<%คําสั่งในภาษา PHP;%>

Page 6: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

รูปแบบการเขียนสคริปต PHP (ตอ)

• ลักษณะพิเศษ<%=$ตัวแปร;

คําสั่งในภาษา PHP;%>

หมายเหตุ 1. ทุกคําสั่งใน PHP ตองปดทายดวย ;2. คําสั่งจะเปนตัวพิมพเล็กหรือใหญก็ได3. ทดสอบโปรแกรม phpex_01.php

Page 7: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ชนิดขอมูล

ชนิด คําอธิบาย

IntegerFloating point NumbersStringArrayObjectType juggling

เลขจํานวนเต็ม

เลขจํานวนจริง

ขอความ

อารเรย หรือ แถวลําดับขอมลูลักษณะออปเจ็กต

ขอมลูลักษณะเฉพาะผูใชเติมเขามา

Page 8: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ตัวแปร (Variable)

• ชื่อแทนขอมูล$varname=value;

• การตั้งชื่อ ขึ้นตนดวย $ และตามดวยตัวอักษร ยาว 1 ถงึ 255 ตัวเชน

$Name = “Panutson”;• นอกจากนี้มีตัวแปรระบบ เชน

$SCRIPT_NAME เปนตัวแปรของระบบแสดงชื่อ script PHP ที่กาํลังทํางาน

Page 9: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คาคงที่ (Constant)

• คาคงที่ที่กําหนดโดย PHP (Built-in Constant) เชนTRUE คาคงที่เปนจริงPHP_VERSION คาคงที่แสดงเวอรชันของ PHP

• คาคงที่ที่ผูใชกําหนดเอง (User-defined Constant)DEFINE(ConstName, Value);เชน

DEFINE(“NewLine”,”<BR>\n”);

Page 10: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ตัวดําเนินการ (Operator)

• Arithmetic Operators

+ บวก $c=$a + $b;

- ลบ $c=$a - $b;* คูณ $c=$a * $b;/ หาร $c=$a / $b;% หารเอาเศษ $c=$a % $b;

Page 11: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ตัวดําเนินการ (Operator) (ตอ)

• Assignment Operators

= กําหนดคา $c = 1;

+= เพิ่มคา $c += 1;-= ลบคา $c -= 1;*= คูณคา $c *= 1;/= หารคา $c /= 1;

Page 12: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ตัวดําเนินการ (Operator) (ตอ)

• Bitwise Operators

& And $a & $b

| Or $a | $b~ Not ~$a<< Shift Left $a << $b>> Shift right $a >> $b

Page 13: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ตัวดําเนินการ (Operator) (ตอ)• Comparison Operators

== เทากับ $a == $b

!= ไมเทากับ $a != $b< นอยกวา $a < $b> มากกวา $a > $b<= นอยกวาหรือเทากับ $a <= $b

>= มากกวาหรือเทากับ $a >= $b

Page 14: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ตัวดําเนินการ (Operator) (ตอ)• Incrementing/Decrementing Operators

• String Operator (.) เชน

$a = “Hello...”;$b = $a.”Students”;

++$a เพิ่มคากอน

$a++ เพิ่มคาทีหลัง

--$a ลดคากอน

$a-- ลดคาทีหลัง

Page 15: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ตัวดําเนินการ (Operator) (ตอ)• Logical Operators

and และ $a and $b

&& และ $a && $bor หรือ $a or $b|| หรือ $a || $bxor exclusive or $a xor $b

! ไม !$a

Page 16: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

การใสหมายเหตุ (Comment)

• ใชเครื่องหมาย // หรือ # ตามดวยขอความหมายเหตุเชน

echo “Hello..<br>”; //แสดงขอความ Hello..• ถามีหมายเหตุหลายบรรทัด ใช /* ตามดวยขอความ แลวปดดวย */

เชน

/* หมายเหตุบรรทัดที่ 1หมายเหตุบรรทัดที่ 2*/

Page 17: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่ง echo หรือ print• คําสั่งแสดงขอความหรือขอมูล

echo “ขอความ”;

หรือ

echo ตัวแปร;print “ขอความ”;

หรือ

print ตัวแปร;

เชน $Fname=“Panutson”;echo “Hello...”.$Fname;print $Fname,” สวัสดี”;

• ทดสอบโปรแกรม phpex_02.php ถึง phpex_05.php

Page 18: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งเกี่ยวกับเงื่อนไข

• If...elseif...elseรูปแบบคําสั่ง

IF (condition1){instructions;}

ELSEIF (condition2){instructions;}

....ELSE

{instructions;}

Page 19: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งเกี่ยวกับเงื่อนไข (ตอ)• Switch...Case

รูปแบบคําสั่ง

Switch (variable)

{

Case expr1;

{instructions;

break;}

Case expr2;

{instructions;

break;}

....

default;

{instructions;}

}

Page 20: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งการทํางานวนซ้ํา (Loop)

• คําสั่ง Forรูปแบบคําสั่ง

For ([start]; [condition]; [step]){

instructions;}

โดย start คือคาเริ่มตน condition คือเงื่อนไข

step คือคาเพิ่มขึ้นหรือลด ถาไมระบุถือวาเพิ่มคาครั้งละ 1

Page 21: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งการทํางานวนซ้ํา (Loop) (ตอ)

• คําสั่ง Whileรูปแบบคําสั่ง

While (condition){

instructions;[step];

}หมายเหตุ step ถามี จะเปนการกําหนดใหคาของ condition

เปลี่ยนไป

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex_10.php�
Page 22: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งการทํางานวนซ้ํา (Loop) (ตอ)

• คําสั่ง Do...Whileรูปแบบคําสั่ง

Do{

instructions;[step];

}While (condition)

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex_11.php�
Page 23: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งการทํางานวนซ้ํา (Loop) (ตอ)

• คําสั่ง Foreachรูปแบบคําสั่ง

Foreach (array_var as value_var){

instructions;}

หมายเหตุ 1. เปนการทํางานวนซ้ํา ตามตัวแปรอารเรย2. ทดสอบโปรแกรม phpex_12.php ซึ่งมีการใชคําสั่ง

While รวมกับ Function list() และ each()แสดงผลเหมือนคําสั่ง Foreach

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex_12.php ซึ่งมีการใช้คำสั่ง While ร่วมกับ Function list() และ each() แสดงผลเหมือนคำสั่ง Foreach �
Page 24: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งควบคุมการทํางานอื่นๆ

• คําสั่ง Break คือหยุดการทํางาน ถาใชกับคําสั่งวนซ้ําจะออกจากการวนซ้ํา รูปแบบคําสั่ง

Break;• คําสั่ง Continue คือทํางานตอ

ถาใชกบัคําสั่ง For จะกลับไปเพิ่มคาใหกับตัวแปรทันทีถาใชกบัคําสั่ง While จะกลับไปทดสอบตามเงื่อนไขทันทีรูปแบบคําสั่ง

Continue;หมายเหตุ ทดสอบโปรแกรม phpex_13.php และ phpex_14.php

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex_13.php และ phpex_14.php�
Page 25: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

คําสั่งควบคุมการทํางานอื่นๆ (ตอ)

• คําสั่ง Include คือคําสั่งนําแฟมอื่นมาทํางานรวมกับ script ท

ี่

กําลังทาํงานอยู รูปแบบคําสั่งInclude(‘file’);

file คือแฟมที่ตองการนํามาแทรกใน script ที่ใชงานอยู

หมายเหตุ ทดสอบโปรแกรม phpex_15.php

Page 26: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ฟงกชันของ PHP• ฟงกที่ผูใชสรางเอง

• ฟงกชันที่มากับ PHP เรียกใชงานไดทันทีฟงกชนัที่ผูใชสรางเอง

- ฟงกชันที่ไมมีการสงคารูปแบบ คอื

Function Func_Name(){

Instructions;}

เรียกใชงานโดย Func_Name();

หมายเหตุ ทดสอบโปรแกรม phpex_16.php

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex_16.php�
Page 27: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ฟงกชันของ PHP (ตอ)ฟงกชนัที่ผูใชสรางเอง (ตอ)

- ฟงกชันที่มีการสงคาระหวางกันรูปแบบ คอื

Function Func_Name(parameter){

return Instructions;}

เรียกใชงานโดย Func_Name(parameter_value);

หมายเหตุ ทดสอบโปรแกรม phpex_17.php

Page 28: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ฟงกชันของ PHP (ตอ)ฟงกชันทีม่ากับ PHP เรียกใชงานไดทันท ีเชนฟงกชัน ความหมาย หมายเหตุ

date()time()Abs(x)Cos(radain)max(x, y,..)min(x, y,..)log(x)rand()

วันเวลาปจจุบัน

เวลาปจจุบัน

คาสัมบูรณ

คา cosineคาสูงสุด

คาต่ําสุด

logarithm ฐานธรรมชาติ (e)

คาสุม (random no.)

อาจกําหนดรูปแบบได

มุม radian

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex_18.php ถึง phpex22.php �
Page 29: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

Format date

• a am/pm• A AM/PM• d 01-31• D Mon-Sun• F January-December• g 1-12• G 0-24• h 01-12

Page 30: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

• H 00-23• i 00-59 (minute)• J 1-31• l Sunday-Satueday• L Leap year 1 or 0• m 01-12• t จํานวนวันในเดือนนั้น

• Y 2005• y 05

Page 31: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ฟงกชันของ PHP (ตอ)ฟงกชันทีม่ากับ PHP เรียกใชงานไดทันท ี(ตอ) เชนฟงกชัน ความหมาย หมายเหตุ

chdir(directory)mkdir(directory)copy(sour,dest)rename(old,new)mail(to,sub,mes,[h])chr(x)substr(str,start,[len])Etc.

เปลี่ยนไปยังสาระบบที่ตองการ

สรางสาระบบ

สําเนาแฟมขอมลู

เปลี่ยนชื่อแฟมขอมลู

สง mailแปลงรหัส ascii เปนอักขระตัดอักขระที่ตองการออกมา

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex_18.php ถึง phpex22.php�
Page 32: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ฟงกชันของ PHP (ตอ)ฟงกชันทีม่ากับ PHP เรียกใชงานไดทันท ีดานแฟมขอมูล เชน

ฟงกชัน ความหมาย

Fopen(filename,mode)Fgetc(file_handle)Feof(file_handle)Fgets(file_handle,len)Rewind(file_handle)Fgetss(file_handle,len)

Fclose(file_handle)

เปดแฟมขอมูล

อานขอมูลทีละอักขระ

ตรวจสอบ end-of-fileอานขอมูลทีละบรรทัดหรือ ตาม lenเลื่อนพอยเตอรไปตนแฟมขอมลู

อานขอมูลทีละบรรทัดหรือ ตาม len โดย ตัดแท็ก html

ปดแฟมขอมลู

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex23.php�
Page 33: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ฟงกชันของ PHP (ตอ)ฟงกชันทีม่ากับ PHP เรียกใชงานไดทันท ี(ตอ) ดานแฟมขอมูล เชน

ฟงกชัน ความหมาย

File(filename)Fputs(filename,text)

Fread(file_handle,len)Fseek(file_handle,offset)Readfile(filename)Unlink(filename)

etc.

อานแฟมขอมูลทีละบรรทัดบันทึกลง arrayเขียนขอความลงแฟม ถาเดิมมีขอมูลอยูจะลบขอมลูเดิม

อานขอมูลจากแฟม ระบุความยาว len ไดเลื่อนพอยเตอรไปขางหนาตามตองการ

อานขอมูลจากแฟมขอมูลโดยไมตองเปดแฟม

ลบแฟมขอมลู

Presenter�
Presentation Notes�
ทดสอบโปรแกรม phpex23.php�
Page 34: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

Int fopen(string filename, string mode )

• filename ชื่อไฟลที่ตองการเปดหรือสรางขึ้นใหม• Mode วิธีการเปดไฟลไดแก

• ‘r’ อานไดอยางเดียวตัวชี้จะเริ่มตําแหนงเริ่มตนไฟล

• ‘r+’ อานและเขียนตัวชี้จะเริ่มตําแหนงเริ่มตนไฟล

• ‘w’ เขียนไดอยางเดียวตัวชี้จะเริ่มตําแหนงเริ่มตนไฟลถามีไฟล

อยูแลวจะลบขอมูลเดิมทั้งหมด ถาไมมีจะสรางขึ้นใหม

• ‘w+’ อานและเขียนไดอยางเดียวตัวชี้จะเริ่มตําแหนงเริ่มตนไฟล

ถามีไฟลอยูแลวจะลบขอมูลเดิมทั้งหมด ถาไมมีจะสราง

ขึ้นใหม

Page 35: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

• ‘a’ เขียนไดอยางเดียวตัวชี้จะเริ่มตําแหนงสิ้นสุดไฟล ถาไมม

จะสรางขึ้นใหม

• ‘a+’ อานและเขียน ตัวชี้จะเริ่มตําแหนงสิ้นสุดไฟล ถาไมม

จะสรางขึ้นใหม

• ตัวอยาง$fxx = @fopen(“c:/php_nuj/test.php”,”r”);

Page 36: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

การอานไฟล

• String fgets (int fp [, int length])fp ไฟลพอยเตอร

length ความยาวของขอความโดยเริ่มจากตําแหนงตัวชี้เทากับ length - 1 หากมระบุ 1024

• String fgetc(int fp)fp ไฟลพอยเตอร

• หากพบ EOFจะคืนคา False

Page 37: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

• String fgetss(int fp, int length [, string allowable_tags])fp ไฟลพอยเตอร

length ความยาวของขอความโดยเริ่มจากตําแหนงตัวชี้เทากับ length - 1 หากมระบุ 1024

allowable_tags ระบุแท็ก HTML ที่ตองการแสดง

Page 38: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

Int fputs(int fp, string str [,int length])

fp ไฟลพอยเตอร

str ขอความที่ตองการเขียนลงไฟล

length ความยาวของขอความที่เขียน

• String fread(int fp, int length)fp ไฟลพอยเตอร

length ความยาวของขอความที่อาน

Page 39: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

PHP กบัฐานขอมูล

• dBase• Informix• Microsoft SQL Server• ODBC โดยใชฐานขอมูล Microsoft Access• Oracle• MySQL• etc.

Page 40: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

PHP กับฐานขอมูล MySQL

ฟงกชัน ความหมาย

mysql_affected_rowsmysql_change_usermysql_closemysql_connectmysql_create_dbmysql_data_seekmysql_db_query

จํานวนระเบียนในการใช กอนหนาเปลี่ยนชื่อผูใช

ปดการติดตอ MySQLติดตอกับ MySQL Serverสรางฐานขอมูล MySQLเลื่อนพอยเตอรภายใน

สงคิวรีไปยัง MySQL

ฟงกชันสําหรับการติดตอฐานขอมูล MySQL

Page 41: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

PHP กับฐานขอมูล MySQL (ตอ)

ฟงกชัน ความหมาย

mysql_drop_dbmysql_errnomysql_errormysql_fetch_arraymysql_fetch_fieldmysql_fetch_lengthsmysql_fetch_object

ลบฐานขอมูล MySQLคาบอกขอผิดพลาดจาก MySQLขอความบอกขอผิดพลาดจาก MySQLคาระเบียนเก็บใน arrayคาขอมูล field เกบ็เปน objectคาขอบเขตแตละผลลัพธ

คาผลลัพธเปน object

ฟงกชันสําหรับการติดตอฐานขอมูล MySQL

Page 42: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

PHP กับฐานขอมูล MySQL (ตอ)

ฟงกชัน ความหมาย

mysql_fetch_rowmysql_field_namemysql_field_seekmysql_field_tablemysql_field_typemysql_field_lenmysql_free_result

คาผลลัพธระบุลง arrayคาผลลัพธเปนชื่อ fieldกําหนดพอยเตอรชี้ไปยัง fieldคาผลลัพธเปนชื่อตารางจากชื่อ fieldผลลัพธเปนประเภทของ fieldขนาดของ field ที่กําหนดทําใหหนวยความจําวางมากขึ้น

ฟงกชันสําหรับการติดตอฐานขอมูล MySQL

Page 43: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

PHP กับฐานขอมูล MySQL (ตอ)

ฟงกชัน ความหมาย

mysql_insert_idmysql_list_fieldsmysql_list_dbsmysql_list_tablesmysql_num_fieldsmysql_num_rowsmysql_pconnect

ไดคา id ทีส่รางจากคําสั่ง insertแสดงชื่อ fieldsแสดงชื่อฐานขอมูลที่มีใน MySQL Server

แสดงชื่อตารางในฐานขอมูล MySQL

คาผลลัพธเปนจํานวน fieldคาผลลัพธเปนจํานวนระเบียน

เปดการติดตอกับ MySQL Server

ฟงกชันสําหรับการติดตอฐานขอมูล MySQL

Page 44: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

PHP กับฐานขอมูล MySQL (ตอ)

ฟงกชัน ความหมาย

mysql_querymysql_resultmysql_select_dbmysql_tablename

สง query ไป MySQLผลลัพธเปนขอมูล

เลือกฐานขอมูล MySQLใหชื่อตาราง

ฟงกชันสําหรับการติดตอฐานขอมูล MySQL

Page 45: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

การติดตอฐานขอมูลและแสดงผล

• เริ่มติดตอฐานขอมูล• เลือกฐานขอมูลที่ตองการ• กําหนดคําสั่ง SQL และสั่งใหทํางาน• เก็บขอมูลลงตัวแปร array• นับจํานวนระเบียน• แสดงผลทาง browser• ปดการติดตอกับฐานขอมูล MySQL

Page 46: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

เริ่มติดตอฐานขอมูล

• ใชฟงกชัน ดังนี้mysql_connect($hostname, $user,

$password) or die(“message”);เชน

$hostname = “localhost”;$user = “teach”;$password = “”;mysql_connect($hostname, $user,

$password) or die(“ติดตอฐานขอมูลไมได”);

Page 47: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

เลือกฐานขอมูลที่ตองการ

• ใชฟงกชัน ดังนี้mysql_select_db($dbname) or die(“message”);เชน

$dbname = “bookshop”;mysql_select_db($dbname) or die(“เลือกฐานขอมูลไมได”);

Page 48: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

กําหนดคําสั่ง SQL และสั่งใหทํางาน

• ใชฟงกชัน ดังนี้mysql_query($sql)

เชน

$tblname = “book”;$sql = “select * from $tblname”;mysql_query($sql);

Page 49: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

เก็บขอมูลลงตัวแปร array• ใชฟงกชัน ดังนี้

mysql_fetch_array($dbname)• โดยมีรูปแบบคําสั่ง เชน$dbquery = mysql_db_query($dbname, “select *

from $tblname”);$result = mysql_fetch_array($dbname);หมายเหตุ $result เปน array ดังนั้น

$result[0] คือสมาชิกตัวแรกของ arrayหรือ

$result[“barcode”] คือ คาของ field ชื่อ barcode

Page 50: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

นับจํานวนระเบียน

• ใชฟงกชัน ดังนี้mysql_num_rows($dbquery)

• จากการเก็บผลลัพธใน array ตามจํานวนระเบียนที่มีอยู ดังนั้นกอน แสดงผลอาจจําเปนตองทราบจํานวนระเบียน

• รูปแบบการใชงาน เชน$dbquery = mysql_db_query($dbname,

“select * from $tblname”);$num_rows = mysql_num_rows($dbquery);

Page 51: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

แสดงผลทาง browser

• รูปแบบคําสั่ง ดังนี้เชน$i=0;while ($i < $num_rows){$result = mysql_fetch_array($dbquery);echo “<tr> <td>$result[“barcode”]</td>

<td>$result[“title”]</td><td>$result[“author”]</td><td>$result[“price”]</td> <td>$result[“type”]</td><td>$result[“year”]</td></tr>;

$i++;}

Page 52: •Professional Home Page •PHP Hypertext Processor •Server ......PHP •Professional Home Page •PHP Hypertext Processor •Server Side Script •

ปดการติดตอกับฐานขอมูล MySQL

• ใชฟงกชัน ดังนี้mysql_close()

หมายเหตุ ทดสอบตัวอยางโปรแกรม phpex_24.php