งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Download PHP. C:\windows\PHP.ini cgi.force_red irect = 0 พิมพ์แทรก.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Download PHP. C:\windows\PHP.ini cgi.force_red irect = 0 พิมพ์แทรก."— ใบสำเนางานนำเสนอ:

1 Download PHP

2

3

4

5

6 C:\windows\PHP.ini cgi.force_red irect = 0 พิมพ์แทรก

7 เปลี่ยนจาก Off เป็น On

8 error_reporting=E_ALL & ~E_NOTICE

9 Internet Programming by PHP

10 PHP Web-Server Architecture Web Server Server Side Client Side PHP engine.PHP Database HTML Page เรียกไฟล์.PHP Web Browser Clien t

11 PHP Tag Styles XML Style Order porocessed. ’; ?> Short Style Order porocessed. ’; ?> SCRIPT Style echo ‘ Order porocessed. ’; ASP Style Order porocessed. ’; %>

12 ความสามารถของ PHP มีความสามารถในการจัดการกับตัวแปรหลาย ประเภท เช่น integer, float, string, array เป็น ต้น สามารถรับข้อมูลจากฟอร์มของ HTML ได้ สามารถรับ - ส่ง Cookies ได้ สามารถใช้ตัวแปรแบบ Session ได้ ( PHP version 4.0 ขี้นไป ) มีความสามารถในการรองรับการเขียนโปรแกรม แบบ OOP (Object Oriented Programming) สามารถเรียกใช้ COM component ได้ มีความสามารถในการติต่อและจัดการฐานข้อมูล มีความสามารถในการสร้างภาพกราฟฟิก

13 New Document <?php echo " ทดสอบ PHP"; ?> ตัวอย่างของรูปแบบการเขียน โปรแกรมแบบ PHP

14 Variable Type Interger => ใช้สำหรับเก็บข้อมูลที่เป็นตัวเลขไม่มีจุด ทศนิยม Double => ใช้สำหรับเก็บข้อมูลที่เป็นตัวเลขจำนวน จริง String => ใช้สำหรับเก็บข้อมูลที่เป็นตัวอักษร Boolean => ใช้สำหรับข้อมูลในการตรวจสอบสถานะ ที่เป็น True หรือ False Array => ใช้สำหรับเก็บข้อมูลที่เป็น Multiple Data โดยสามารถใช้ได้กับข้อมูล ทุกชนิดที่กล่าวมา Object => ใช้สำหรับเก็บข้อมูลในรูปแบบของ Classes

15 การประกาศตัวแปร ต้องมีเครื่องหมาย $ (dollar sing) นำหน้าเสมอ ใน PHP การประกาศตัวแปรด้วยตัวพิมพ์เล็ก หรือ ตัวพิมพ์ใหญ่จะถือว่าเป็นคนละตัว แปร เช่น number กับ Number ชื่อตัวแปรจะต้องขึ้นต้นด้วย ตัวอักษร หรือ เครื่องหมาย _ (Underscore) เท่านั้น การประกาศตัวแปรใน PHP ไม่ต้องกำหนด Type ของตัวแปร PHP จะเป็นตัวกำหนด ให้โดยอัตโนมัติ โดยพิจารณาจากค่าที่จะนำมา เก็บในตัวแปรนั้น ยกเว้นตัวแปรชนิด Array และ Object

16 <?php $VariablesType = " ตัวเลข "; // ตัวแปร String $variablestype = 10; // ตัวแปร Interger เลขฐาน 10 $number8 = 025; // ตัวแปร Interger เลขฐาน 8 $number16 = 0x25; // ตัวแปร Interger เลขฐาน 16 $NumberReal1 = 9.18; // ตัวแปร Double แสดงจุดทศนิยม $NumberReal2 = 9.18e3; // ตัวแปร Double เลขยกกำลัง 9.81 คูณ 10 ยกกำลัง 3 $NumberReal3 = 9.18e-3; // ตัวแปร Double เลขยกกำลัง 9.81 คูณ 10 ยกกำลังลบ 3 echo "$VariablesType ";echo "$variablestype "; echo "$number8 ";echo "$number16 "; echo "$NumberReal1 ";echo "$NumberReal2 ";echo "$NumberReal3 "; ?> EX ตัวอย่างรูปแบบการ ประกาศตัวแปร out put

17 การเก็บค่าลงใน Array <?php $VariablesArray = array("10","20","30"); echo $VariablesArray[0]." "; echo $VariablesArray[1]." "; echo $VariablesArray[2]." "; ?> <?php $VariablesArray = array('name'=>' สุระชัย ', 'Lname'=>' หัวไผ่ ','Email'=>'surachai@rbac.ac.th'); echo $VariablesArray['name']." "; echo $VariablesArray['Lname']." "; echo $VariablesArray['Email']." "; ?> แบบที่ 1 แบบที่ 2 out put

18 การแสดงค่าจาก Array <?php $VariablesArray = array('name'=>' สุระชัย ','Lname'=>' หัวไผ่ ','Email'=>'surachai@rbac.ac.th'); ?> <?php // แสดงค่าโดยใช้ function each() $VariablesArray1 = each($VariablesArray); echo $VariablesArray1['0']." "; echo $VariablesArray1['1']." "; $VariablesArray1 = each($VariablesArray); echo $VariablesArray1['0']." "; echo $VariablesArray1['1']." "; $VariablesArray1 = each($VariablesArray); echo $VariablesArray1['0']." "; echo $VariablesArray1['1']." "; ?> function each() out put

19 function list() การแสดงค่าจาก Array <?php $VariablesArray = array(' สุระชัย ',' หัวไผ่ ','surachai@rbac.ac.th'); ?> <?php // แสดงค่าโดยใช้ ้ function List() list($name,$last,$email)=$VariablesArray; echo " ชื่อ $name นามสกุล $last email $email \n"; ?> out put

20 Arithmetic Operators + การบวก (addition) - การลบ (subtraction) * การคูณ (multiplication) / การหาร (division) % โมดูลัส (modulus) เศษ ที่ได้จากการหาร + + เพิ่มค่าขึ้น 1 (incrementing) - - ลดค่าลง 1 (decrementing) $a=$b+$ c ตัวอย่า ง Echo ($a+$b); $c=$a%$b; $a+ + ++$ a นำค่าตัวแปรมาใช้ก่อนค่อยเพิ่มค่า เพิ่มค่าก่อนค่อยนำค่าตัวแปรมาใช้ $a + =$b $a =$a +$b เทียบเท่า

21 Logical Operator && หรือ and || หรือ or ! AN D OR NO T ^ หรือ xor Exclusive OR Operator ความห มาย ตัวอ ย่าง $a && $b $a || $b !$ a $a ^ $b

22 Comparison Operators = = หมายความว่า เท่ากับ (equal) = = = หมายความว่า เหมือนกัน (identical) != หรือ <> หมายความว่า ไม่เท่ากับ (not equal) !== หมายความว่า ไม่เหมือนกัน (not identical) < หมายความว่า น้อยกว่า (Less then) > หมายความว่า มากกว่า (Greater than) <= หมายความว่า น้อยกว่าเท่ากับ >= หมายความว่า มากกว่าเท่ากับ หมายเหตุ $a<$b หมายความว่า a น้อยกว่า b

23 Conditional Execution if <?php $a=1;$b=2; if ($a<$b) { echo "จริง"; } ?> out put If Else <?php $a=1;$b=2; if ($a>$b) {echo "จริง";} else {echo ”เท็จ";} ?> out put

24 Conditional Execution If Elseif <?php $a=1;$b=2; if ($a>$b) {echo "จริง";} elseif($a<$b) {echo "เท็จ";} ?> out put Switch <?php $a=1; switch ($a){ case 1: echo “1”; break; case 2: echo ”2"; break; } ?> out put

25 Iteration For <?php For ($a=0; $a<=5; $a++){ echo “$a”; } ?> out put While <?php $a=1; While ($a<=5){ echo "$a"; $a++; } ?> out put

26 Iteration Do...Whi le <?php $a=1; do{ echo "$a"; $a++; }While ($a<=6); ?> out put <?php $a=1; While ($a<=5){ echo "$a"; $a++; if ($a==3){break;} } ?> Break out put

27 การรับค่าจาก web browser แบบ Post ชื่อ : เพศ : ชาย หญิง ระดับการศึกษา : ประถมศึกษา มัธยมศึกษา อนุปริญญา ปริญญาตรีขึ้นไป

28 การแสดงค่าที่รับจาก web browser ด้วย PHP <?php echo " ค่าของช่องรับข้อความ : ". $txtname. " "; echo " ค่าของปุ่มตัวเลือก : ". $gender. " "; echo " ค่าของลิสต์บ็อกซ์ : ". $graduation. " "; echo " ค่าของ hidden field : ". $myname. " "; ?>

29 การรับค่าจาก web browser แบบ Get ชื่อ : เพศ : ชาย หญิง ระดับการศึกษา : ประถมศึกษา มัธยมศึกษา อนุปริญญา ปริญญาตรี

30 การแสดงค่าที่รับจาก web browser ด้วย PHP <?php echo "query string ที่พ่วงมากับ URL คือ ". $QUERY_STRING. " "; echo " ค่าของช่องรับข้อความ : ". $txtname. " "; echo " ค่าของปุ่มตัวเลือก : ". $gender. " "; echo " ค่าของลิสต์บ็อกซ์ : ". $graduation. " "; echo " ค่าของ hidden field : ". $myname. " "; ?>

31 การส่ง query string ผ่าน Hyperlink

32 send <?php echo. $Name. " "; echo. $Email. " "; echo. $Phone. " "; ?>

33 Session Session เป็นตัวแปร เพื่อเก็บค่าต่างๆไว้ ที่ Client side Session จะจำค่าตัวแปรไว้จนกว่าจะปิด หน้าต่างของ Web browser PHP ที่สามารถใช้ตัวแปร Session ได้ ตั้งแต่ version 4.0 ขึ้นไป เราใช้ตัวแปร Session ช่วยในการเก็บค่า Password หรือใช้ช่วยคำนวณจำนวนและราคาสินค้าที่ ต้องการสั่งซื้อ

34 Session Function Session_registor(“ ชื่อตัวแปร ”); ===> ใช้ กำหนดค่าให้ session Session_Start(); ===> ใช้เริ่มต้นการทำงาน ของ session Session_Unregister(“ ชื่อตัวแปร ”); ===> ยกเลิก ตัวแปร session ที่กำหนด Session_destroy(); ===> ยกเลิกตัวแปร session ทั้งหมด

35 Session Example <?php session_start(); session_unregister("username"); ?>

36 Session Example <?php session_register("username"); echo $username; ?> session_file3.php <?php session_start(); echo $username; ?> session_file1.php

37 Cookie Cookie การเก็บข้อมูลฝัง Client Side Cookie จะถูกจัดเก็บในรูปของไฟล์ Cookie ไฟล์ Cookie จะถูกจัดเก็บไว้ที่เครื่อง Client ที่เข้าไปใช้บริการ Cookie สามารถกำหนดอายุการทำงานได้ เราใช้ Cookie ในการตรวจสอบและติดตาม การใช้บริการต่างของผู้ใช้บริการ web server ได้ หรืออาจประยุกต์ใช้ในการให้บริการกับลูกค้า ประจำได้

38 Cookie Function Setcookie( ชื่อ, ค่า, เวลาหมดอายุ ); ===> ใช้สำหรับ ส่ง Cookie ไปยังเครื่องผู้ใช้ Output Buffering Function Ob_start(); ===> ใช้เปิดการทำงานของ Output buffering และจอง buffering ไว้ ( ควรเรียกใช้ function ในบรรทัดแรก ของโปรแกรม ) Ob_end_flush(); ===> ใช้ส่งค่าที่เก็บใน buffer ไปยัง web browser และ ปิดการทำงานของ Output buffering

39 Cookie Example <?php // กำหนดให้ cookie หมดอายุหลังจากถูกสร้างแล้วเป็นเวลา 60 วินาที (1 นาที ) $c_name = "TestCookie"; $c_val = "123"; setcookie($c_name, $c_val, time() + 60); echo " c_name=$c_name "; echo " c_val=$c_val "; ?> ตรวจสอบค่าของ cookie

40 Cookie Example Output ก่อน 60 วินาที Output หลัง 60 วินาที <?php if (isset($HTTP_COOKIE_VARS['TestCookie'])) { echo $HTTP_COOKIE_VARS['TestCookie']; } else { echo " ไม่พบตัวแปร \$HTTP_COOKIE_VARS['TestCookie']"; } ?>

41 Cookie Example <?php echo " ทดสอบ "; setcookie("Test", "456", time() + 60); ?>

42 Management File & Directory for PHP Function ==> fopen() fopen (“ filename”, “Mode”) Mode r ==> เปิดอ่านอย่างเดียวโดยเริ่มอ่านจากตำแหน่งเริ่มต้นของไฟล์ r+ ==> เปิดอ่านและเขียนไฟล์ โดยเริ่มอ่านจากตำแหน่งเริ่มต้นของไฟล์ w ==> เขียนไฟล์ทับไฟล์เดิมข้อความเดิมจะหายหมด ถ้าไม่มีไฟล์อยู่จะ เขียนไฟล์ขึ้นมาใหม่ w+ ==> เปิดเพื่ออ่านไฟล์และ เขียนไฟล์ทับไฟล์เดิมข้อความเดิมจะหาย หมด ถ้าไม่มีไฟล์อยู่จะเขียนไฟล์ขึ้นมาใหม่ a ==> เขียนต่อท้ายไฟล์ ถ้าไม่มีไฟล์อยู่จะเขียนไฟล์ขึ้นมาใหม่ a+ ==> เปิดเพื่ออ่านไฟล์และ เขียนต่อท้ายไฟล์ ถ้าไม่มีไฟล์อยู่จะเขียน ไฟล์ขึ้นมาใหม่ รูปแบบ ==> ใช้เเปิด file

43 Function ==> fclose() fclose ( “file_ID”) รูปแบบ ==> File_ID ==> คือหมายเลขไฟล์ที่ทำการเปิดหลังใช้ fopen() ใช้เปิด file

44 Function ==> fpassthru() เป็นฟังก์ชั่นที่ใช้อ่านข้อมูลทั้งหมดจากไฟล์และปิดไฟล์โดยไม่ต้องใช้ fclose() <?PHP $file1 = fopen("filephp.txt","r"); echo $file1; fpassthru ($file1); ?>

45 Function ==> fread() fread(“file_ID”,” จำนวน Byte ที่ต้องการอ่าน ”) รูปแบบ ==> เป็นฟังก์ชั่นที่ใช้อ่านข้อมูลจากไฟล์โดยกำหนดจำนวนไบต์ได้ <?PHP $file1 = fopen("filephp.txt","r"); $mydata = fread($file1,5); echo $mydata." "; fclose ($file1); ?>

46 Function ==> file() เป็นฟังก์ชั่นพิเศษที่ใช้อ่านข้อมูลจากไฟล์โดยไม่ต้องสั่งเปิดไฟล์ด้วย ฟังก์ชัน fopen() file(“ ชื่อไฟล์ ”) รูปแบบ ==> <?php $data1 = file(“C:/inpub/wwwroot/test.txt”) for ($I=0; $I<Count($data); $i++){ echo $data1.” :”; } ?> aaa bbb ccc

47 การเขียนข้อมูลลง file fputs() fwrite() การเขียนข้อมูลลง file ใน PHP มี 2 function ใช้งาน เหมือนกัน fputs(“File_ID”,” ข้อมูลที่ต้องการเขียนลงไฟล์ ”) รูปแบบ ==> <?php $file1 = fopen("filephp.txt","r"); fputs($file1,”ทดสอบ”); fclose ($file1);} ?>

48 PHP Connection to ODBC 1. สร้างฐานข้อมูล 2. ทำการเชื่อมต่อ ODBC ของ Microsoft Windows กับ ฐานข้อมูลที่ สร้างขึ้น 3. กำหนดชื่อ ของ DSN (Data Source Name) ที่ใช้ สำหรับเชื่อมต้อฐานข้อมูล 4. ใช้ Function ที่มีอยู่ใน PHP Engine เรียกใช้ฐานข้อ มูลผ่าน DSN ที่กำหนดไว้

49 รูปแบบการเชื่อมต่อฐานข้อมูล 1. เปิดฐานข้อมูล ==> Connection 2. จัดการฐานข้อมูล ==> Insert,Update,Delete,Search 3. ปิดฐานข้อมูล ==>Close

50 ฟังก์ชัน odbc_Connect() $conn=odbc_Connect(“DSN”,”Usename”,”Password”); รูปแบบ ใช้สำหรับการเชื่อม ฐานข้อมูล

51 ฟังก์ชัน odbc_Close() odbc_Close(“Connection_ID”); รูปแบบ ใช้สำหรับยกเลิกการเชื่อมต่อ ฐานข้อมูล odbc_Close($conn); Ex

52 ฟังก์ชัน odbc_exec() odbc_exec(“Connection_ID”,”SOL_Statement”); รูปแบบ ใช้สำหรับการสร้าง Query กับ ฐานข้อมูลที่เชื่อมต่อ $sql = “SELECT * From Product Where ProdcutID=$searchID” $table_product = odbc_exec($conn,$sql); Ex

53 ฟังก์ชัน odbc_num_rows() odbc_num_rows(“Execute_Num”); รูปแบบ ใช้สำหรับการนับจำนวน Record ถ้าทำงานไม่ สำเร็จจะคืนค่า -1 กลับมา $sql = “SELECT * From Product Where ProdcutID=$searchID” $table_product = odbc_exec($conn,$sql); $record_count=odbc_num_rows($table_product ); Ex

54 ฟังก์ชัน odbc_num_fields() odbc_num_fields(“Execute_Num”); รูปแบบ ใช้สำหรับการนับจำนวน field ถ้าทำงานไม่ สำเร็จจะคืนค่า -1 กลับมา $sql = “SELECT * From Product Where ProdcutID=$searchID” $table_product = odbc_exec($conn,$sql); $field_count= odbc_num_fields($table_product ); Ex

55 ฟังก์ชัน odbc_fields_name() odbc_fields_name(“Execute_Num”,” field_Id”); รูปแบบ ใช้สำหรับการแสดงชื่อของ field $sql = “SELECT * From Product Where ProdcutID=$searchID” $table_product = odbc_exec($conn,$sql); echo odbc_fields_name($table_product,”1”); Ex

56 ฟังก์ชัน odbc_fetch_row() odbc_fetch_row(“Execute_Num”); รูปแบบ ใช้สำหรับการอ่านข้อมูลใน Record ปัจจุบัน และเลื่อน Index ไปยังตำแหน่งถัดไป $sql = “SELECT * From Product Where ProdcutID=$searchID” $table_product = odbc_exec($conn,$sql); echo odbc_fetch_row($table_product,); Ex

57 ฟังก์ชัน odbc_result() odbc_result(“Execute_Num”,”field_name หรือ field_ID”); รูปแบบ ใช้สำหรับการอ่านข้อมูลใน Record ปัจจุบัน โดยกำหนด field ที่ต้องการได้และเลื่อน Index ไปยังตำแหน่งถัดไป $sql = “SELECT * From Product Where ProdcutID=$searchID” $table_product = odbc_exec($conn,$sql); echo odbc_result($table_product,”ProductName”); Ex

58 Introduction to PHP & MySQL David Olsen WVU Web Services April 2, 2003

59 Overview of Presentation Introduction – My Experience – What I Won’t be Covering Why Use PHP & MySQL How It All Works Overview of PHP PHP Crash Course Overview of MySQL MySQL Crash Course Using PHP to Query a MySQL Database Example Script – Oscar Pool PHP & MySQL Resources

60 Introduction My Experience – Web Developer for 4 years – Started creating DB-driven sites in 2001 (PHP & Sybase) – Not formally trained in programming or database development and have picked it up as I’ve gone along. – Consider myself at an Intermediate-level with PHP & MySQL What I won’t be Covering – Installation – Product Comparisons (e.g. PHP vs. ASP, ColdFusion, Perl) – Web Application Security

61 Why Use PHP & MySQL If you want to add dynamic content to your pages If you want to make your pages easier to maintain If you’re learning your first "real" computing language If you need a solution that’s portable across multiple platforms (e.g. Red Hat Linux to Windows 2000) If you like free software or need a free solution Examples of uses of PHP & MySQL: – Sign-up Forms – Surveys – Polls – Email a Postcard – Content Management

62 Overview of PHP Open Source server-side scripting language designed specifically for the web. Can also be used for command-line scripting and writing client- side GUI applications. Conceived in 1994, now used on +10 million web sites. Outputs not only HTML but can output XML, images (JPG & PNG), PDF files and even Flash movies (using libswf and Ming) all generated on the fly. Can write these files to the filesystem. Supports a wide-range of databases (20 + ODBC). PHP also has support for talking to other services using protocols such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP. Perl- and C-like syntax. Relatively easy to learn. Website @ http://www.php.net/http://www.php.net/

63 PHP Crash Course Embedding PHP in HTML Adding dynamic content – Introduction to PHP functions – How to find system information – Date( ) function Variables Accessing Form Variables Operators Control Structures

64 PHP Crash Course (cont.) Embedding PHP in HTML Hello World! <? echo ‘My name is Dave Olsen!’; ?> PHP tag styles: XML:, Short:, ASP: Script: url: http://www.usablecode.com/wvuPresentation/embed.phphttp://www.usablecode.com/wvuPresentation/embed.php

65 PHP Crash Course (cont.) Adding dynamic content by adding the date to the page. Hello World! <? echo ‘Today is’; echo date(‘H:i jS F’); ?> Date() http://www.php.net/manual/en/function.date.phphttp://www.php.net/manual/en/function.date.php PHP Function Reference http://www.php.net/manual/en/funcref.phphttp://www.php.net/manual/en/funcref.php One useful function is phpinfo(). Gives system information so you can quickly find out what’s on your machine. url: http://www.usablecode.com/wvuPresentation/date.phphttp://www.usablecode.com/wvuPresentation/date.php

66 PHP Crash Course (cont.) Variables: Are the symbols we use to represent data. Variable names can be of any length; can include letters, numbers and underscores; cannot start with a digit; case-sensitive; and can have the same name as a function. To assign values to variables: $foo = ‘bar’; Data Type: String $foo = 1; Data Type: integer $foo = 5.34; Data Type: Double $foo = array(“bar”,”united”); Data Type: Array Data Types are automatically assigned though you can force a data type by type casting. For example: $foo = ‘Hello’; $bar = (int)$foo; $bar now equals 0 Almost all variables are local. Globals include $_POST

67 PHP Crash Course (cont.) Accessing Form Variables Three methods – Short: $varfoo, – Medium: $_POST[‘varfoo’], (recommended for versions of PHP +4.1) – Long: $HTTP_POST_VARS[‘varfoo’] Need register_globals ON for short method to work. Not expected to be ON on the WVU server. I’m open to input on the issue. Tip: For checkbox variables your variable name should end with [ ] – Checkbox results are automatically put into an array – Example:

68 PHP Crash Course (cont.) Operators: Operators are symbols that you can use to manipulate values and variables by performing an operation on them. Web Site @ http://www.php.net/manual/en/language.operators.phphttp://www.php.net/manual/en/language.operators.php Includes: – Assignment (e.g. =, +=, *=) – Arithmetic (e.g. +, -, *) – Comparison (e.g., >=, ==) – Logical (e.g. !, &&, ||)

69 PHP Crash Course (cont.) Control Structures: Are the structures within a language that allow us to control the flow of execution through a program or script. Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops). Example if/elseif/else statement: if ($foo == 0) { echo ‘The variable foo is equal to 0’; } else if (($foo > 0) && ($foo <= 5)) { echo ‘The variable foo is between 1 and 5’; } else { echo ‘The variable foo is equal to ‘.$foo; } Good code will use indents and comments.

70 Overview of MySQL Relational database management system (RDBMS) Free Website @ http://www.mysql.com/

71 MySQL Crash Course Database Basics Common SQL Statements – INSERT – SELECT – UPDATE – DELETE Simple Join Entity-Relationship (ER) Modeling An Easy Way to Manage Your MySQL DBs

72 MySQL Crash Course (cont.) Database Basics A relational database manager (MySQL) manages databases which holds tables which has records (rows) with attributes (columns) Each record must have a unique ID, also known as a Primary Key. When used as an identifier in another table it’s called a Foreign Key. Used for joins. Each attribute has to have a data type. (e.g. int, text, varchar) A database language (SQL) is used to create and delete databases and manage data

73 MySQL Crash Course (cont.) Table structure for following examples: Created two tables, ‘oscarpool’ & ‘bestdirector’ Instead of using SQL to create tables use phpMyAdmin url: http://www.phpmyadmin.net/ CREATE TABLE oscarpool ( uid int(4) auto_increment, username varchar(255), email varchar(255), bestpicture int(2), PRIMARY KEY (uid) ) CREATE TABLE bestdirector ( bdid int(4) auto_increment, name varchar(255), PRIMARY KEY (bdid) )

74 MySQL Crash Course (cont.) Common SQL Statement: INSERT INSERT INTO oscarpool (username,email,bestpicture) VALUES (‘dolsen',‘dave@usablecode.com',1) Creates a new record in the table ‘oscarpool’ Text fields need to have ‘s. Tip: If you have an ‘ in your data you need to escape it before inserting it. Can use the PHP function addslashes(). Example: ‘John O\’Brien’

75 MySQL Crash Course (cont.) Common SQL Statement: SELECT SELECT uid,username FROM oscarpool Selects the attributes ‘uid’ and ‘username’ from every record in ‘oscarpool’ SELECT is how you query the database. You can also: – limit the number of records returned with LIMIT, – limit retrieval to those records that match a condition with WHERE, – sort the data after the query has been evaluated using ORDER BY Tip: To easily select every attribute replace ‘uid’ with ‘*’

76 MySQL Crash Course (cont.) Common SQL Statement: UPDATE UPDATE oscarpool SET email = ‘david.olsen@mail.wvu.edu’ WHERE uid = 1 Updates the email address where ‘uid = 1’ in the table ‘oscarpool’ In this case I know that uid 1 is what my record was. In many cases you’d pass a uid variable from a form.

77 MySQL Crash Course (cont.) Common SQL Statement: DELETE DELETE FROM oscarpool WHERE uid = 1 Deletes the record where ‘uid = 1’ in the table ‘oscarpool’ DELETE only removes the record from the table. To remove an entire table from the database you need to use the SQL statement DROP. Tip: To remove every record in a table but not remove the table just don’t include the WHERE clause.

78 MySQL Crash Course (cont.) Simple Join SELECT bd.name FROM oscarpool op, bestdirector bd WHERE op.uid = 1 and op.bestdirector = bd.bdid Selects the name of the Best Director that the user with ‘uid = 1’ has chosen bestdirector is a Foreign Key of the Primary Key for the table BestDirector Tip: Try to not have fields from two different tables have the same name. Gets confusing when trying to output this data when we connect with PHP.

79 MySQL Crash Course (cont.) Entity-Relationship (ER) Modeling ER Modeling is the simple and clear method of expressing the design (relations) of a database between tables and attributes. Rectangles – Represent entities. Diamonds – Represent relationships between entities Ellipses – Represent attributes that describe an entity Lines – Connect entities to relationships. Can have annotation. M = many, 1 = one. Lines – Connects entities to attributes. No annotation. Entity = Table, Attributes = Attributes Web Database Applications by O’Reilly Publishers gives decent overview

80 MySQL Crash Course (cont.) An easy way to manage your DBs phpMyAdmin is a browser-based administration tool for MySQL. Needs to be installed on your server. It can: – create and drop databases – create, copy, drop, rename and alter tables – delete, edit and add fields – manage keys on fields – load text files into tables – create (*) and read dumps of tables – and more Download phpMyAdmin @ http://www.phpmyadmin.net/http://www.phpmyadmin.net/ Note: phpMyAdmin does not come with out-of-the-box security. You either need to edit the config files to authenticate from a table or use.htaccess.

81 Using PHP to Query a MySQL Database A List of Users Who Have Signed Up For OscarPool <? $dbh = mysql_connect("localhost",“dbusername",“dbpassword") or die(“Couldn't connect to database."); $db = mysql_select_db(“dbname", $dbh) or die(“Couldn't select database."); $sql = “SELECT username, email FROM oscarpool”; $result = mysql_query($sql, $dbh) or die(“Something is wrong with your SQL statement."); while ($row = mysql_fetch_array($result)) { $username = $row[‘username’]; $email = $row[‘email’]; echo ‘ ’.$username.’ \n’; } ?>

82 Using PHP to Query a MySQL Database (cont.) Notes for previous slide example: – The first option in mysql_connect can be an IP address. – mysql_query returns a small table with your results in it. The while loop then goes through each record of that small table and pulls out the attributes/fields you selected in your SQL statement. – die( ) will kill the script. Make sure that that text is informative. – If you use a function in your SQL query then it has to be a part of the $row statement. For example, UNIX_TIMESTAMP(datefield) would be $row[‘UNIX_TIMESTAMP(datefield)’] – \n stands for a new line so that your source code will look a little neater: – PHP MySQL functions @ url: http://www.php.net/manual/en/ref.mysql.phphttp://www.php.net/manual/en/ref.mysql.php

83 Example Script – Oscar Pool Script allows a user to enter a Pool where they get to choose who they think will win in various categories from the 2003 Oscars. The script makes sure that there’s one entry per email address. Utilizes include() to separate logic from presentation. Utilizes custom functions to handle errors and database connections. Not the best in regards to security but not bad. Created in one evening. Graphic Design by Jeph Christoff. url: http://www.usablecode.com/OscarPool/index.phphttp://www.usablecode.com/OscarPool/index.php src url: http://www.usablecode.com/OscarPool/index.phpshttp://www.usablecode.com/OscarPool/index.phps

84 PHP & MySQL Resources Web Sites – http://www.php.net/ http://www.php.net/ – http://www.phpbuilder.com/ http://www.phpbuilder.com/ – http://www.devshed.com/ http://www.devshed.com/ – http://www.phpmyadmin.net/ http://www.phpmyadmin.net/ – http://www.hotscripts.com/PHP/ http://www.hotscripts.com/PHP/ – http://www.mysql.com/ http://www.mysql.com/ – http://www.owasp.org/ http://www.owasp.org/ Books – PHP and MySQL Web Development 2 nd Edition, Welling & Thomson – Web Database Applications with PHP & MySQL, O’Reilly Publishers – PHP Cookbook, O’Reilly Publishers – MySQL Cookbook, O’Reilly Publishers Listservs – thelist, http://lists.evolt.org/ (Note: very general and large volume of email)http://lists.evolt.org/ People - Me, david.olsen@mail.wvu.edu


ดาวน์โหลด ppt Download PHP. C:\windows\PHP.ini cgi.force_red irect = 0 พิมพ์แทรก.

งานนำเสนอที่คล้ายกัน


Ads by Google