1 / 38

PHP-4 ติดต่อกับฐานข้อมูล MS- Ac c ess

PHP-4 ติดต่อกับฐานข้อมูล MS- Ac c ess. โหลดฐานข้อมูลที่. http://www.iamsanya.com/docfile/database1.zip. เริ่มต้นการติดต่อกับ Access. ก่อนที่เราจะติดต่อกับฐานข้อมูลจริง ต้องรู้จักกับฟังก์ชั่นเหล่านี้ก่อน ซึ่งมีความจำเป็นมาก ๆ ในการใช้งาน.

Download Presentation

PHP-4 ติดต่อกับฐานข้อมูล MS- Ac c ess

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP-4ติดต่อกับฐานข้อมูล MS-Access โหลดฐานข้อมูลที่ http://www.iamsanya.com/docfile/database1.zip

  2. เริ่มต้นการติดต่อกับ Access • ก่อนที่เราจะติดต่อกับฐานข้อมูลจริง ต้องรู้จักกับฟังก์ชั่นเหล่านี้ก่อน ซึ่งมีความจำเป็นมาก ๆ ในการใช้งาน

  3. การที่เราจะเขียน php ติดต่อกับฐานข้อมูล Microsoft Access เราจะต้องติดตอผ่าน ODBC ซึ่งเป็น Driver ของการเชื่อมต่อฐานข้อมูล ซึ่งการติดต่อผ่าน ODBC จะต้องมีการ Set ชื่อ DSN เพื่อเป็นชื่ออ้างอิงในการติดต่อกับฐานข้อมูลนั้น ๆ สามารถศึกษาได้ที่หัวข้อถัดจากนี้

  4. การ SET DSN เพื่อติดต่อกับ ACCESS ก่อนที่เราจะ Set Dsn เราต้องมีฐานข้อมูลก่อน รู้จะ Path และ ที่อยู่ของฐานข้อมูลในที่นี้ ฐานข้อมูลผมอยู่ที่ C:\Appserv\www\web\database.mdb เริ่มกันเลยครับ ผมจะแบ่งเป้น 2 ประเภทครับ คือ - การ Set บน Win95,Win98,WinMeเมนู Start > Sitting > Control Panel เลือก ODBC Datasources (32Bit) 1. ดับเบิ้ลคลิก

  5. 2. เลือก Add จากนั้นเลือก Microsoft Access Driver (*.mdb) -> Finish

  6. กำหนด ชื่อ DSN เป็น customer และ เลือก Select เพื่อ ทำการอ้างอิงไฟล์ฐานข้อมูล

  7. - การ Set บน WinXP,NT,2000 เมนู Start > Sitting > Control Panel > Administrative Tool เลือก Data Sources (ODBC) 1. ดับเบิ้ลคลิก เลือก Tab System DSN และเลือก > Add จากนั้นเลือก Driver do Microsoft Access Driver (*.mdb) -> Finish

  8. กำหนด ชื่อ DSN เป็น customer และ เลือก Select เพื่อ ทำการอ้างอิงไฟล์ฐานข้อมูล • จะได้ชื่อ DSN เป็น customer ใว้อ้างอิงในการติดต่อกับฐานข้อมูล

  9. ตอนที่ 1 การแสดงคุณสมบัติต่าง ๆ ของตาราง ฐานข้อมูล C:\Appserv\www\web\database.mdb ซื่อตาราง Customer

  10. การแสดงชื่อและจำนวนฟิวส์ของตาราง customer php4-1.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้"); $sql = "select * from customer";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");$num_field = odbc_num_fields($execute);for ($I=1; $I<= $num_field; $I++){echo $I. ". ฟิวส์ <b> ".odbc_field_name($execute, $I),"</b> ชนิดของข้อมูลเป็น <b> “.odbc_field_type($execute, $I),"</b><br>"; }odbc_close($connect);?> Out Put 1. ฟิวส์ id ชนิดของข้อมูลเป็น COUNTER2. ฟิวส์ name ชนิดของข้อมูลเป็น VARCHAR3. ฟิวส์ surname ชนิดของข้อมูลเป็น VARCHAR

  11. ตอนที่ 2 การนำข้อมูลจากตารางมาแสดง ฐานข้อมูล C:\Appserv\www\web\database.mdb ชื่อตาราง customer

  12. php4-2.php การนำข้อมูลจากตารางมาแสดง <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้"); $sql = "select * from customer";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");?><table border="1" width="371"><tr> <td width="32"> <div align="center">id</div></td><td width="132"> <div align="center">name</div></td><td width="185"> <div align="center">surname</div></td></tr><? $i=1;while(odbc_fetch_row($execute,$i)){

  13. $id=odbc_result($execute,"id");$name=odbc_result($execute,"name");$surname=odbc_result($execute,"surname");?><tr><td width="35"> <div align="center"> <? echo "$id"; ?></div></td><td width="129"> <? echo "$name"; ?></td><td width="184"> <? echo "$surname"; ?></td></tr></table><?$i++;}?><?odbc_close($connect);?> <? if ($i%2==0) { echo "<tr bgcolor=#FFFF99>"; }else{ echo "<tr bgcolor=#FFCCCC>"; }?> //เพื่อสลับสีแถว ให้เขียนทับ <tr>

  14. Out Put

  15. ตอนที่ 3 การเพิ่มข้อมูลลงในตาราง ฐานข้อมูล C:\Appserv\www\web\database.mdb

  16. รูปแบบ insert ino ชื่อตาราง (ฟิวส์1,ฟิวส์2,ฟิวส์3...) values ('ค่า1','ค่า2','ค่า3',...) php4-3.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้");/* จะเห็นว่า code ข้างล่างไม่ได้มีการเพิ่มฟิวส์ id เพราะ id ชนิดของข้อมูลเป็น Auto number */ $sql = "insert into customer (name,surname) values ('นายวีระชัย ','นุกิจรัมย์')";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");odbc_free_result($execute);odbc_close($connect);?>

  17. Add ผ่าน Form php4-4.php <html><body><form name="form1" method="post" action=“php4-4-2.php”>Name <input type="text" name="name">Surname <input type="text" name="surname"><input type="submit" name="Submit" value="Submit"></form></body></html>

  18. php4-4-2.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้");/* จะเห็นว่า code ข้างล่างไม่ได้มีการเพิ่มฟิวส์ id เพราะ id ชนิดของข้อมูลเป็น Auto number */ $sql = "insert into customer (name,surname)values (‘$name’,’$surname’)";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");odbc_free_result($execute);odbc_close($connect);echo"ได้ทำการบันทึกข้อมูลเรียบร้อยแล้ว ";?>

  19. ตอนที่ 4 การค้นหาข้อมูลจากตาราง • ฐานข้อมูล C:\Appserv\www\web\database.mdb

  20. php4-5.php <html><body><form name="form1" method="post" action="">กรุณากรอกชื่อที่ต้องการค้นหา <input type="text" name="search_name" value="<?=$search_name;?>"><input type="submit" name="Submit" value="Search"></form><?if(empty($search_name)){?><hr>กรุณากรอกข้อมูลด้วยครับ<?}else{$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้"); $sql = "select * from customer Where name like '%$search_name%'"; $execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");?>

  21. php4-5.php ต่อ <table border="1" width="371"><tr> <td width="32"> <div align="center">id</div></td><td width="132"> <div align="center">name</div></td><td width="185"> <div align="center">surname</div></td></tr><? $i=1;while(odbc_fetch_row($execute,$i)){ $id=odbc_result($execute,"id"); $name=odbc_result($execute,"name"); $surname=odbc_result($execute,"surname");?>

  22. php4-5.php ต่อ <table border="1" width="370"><tr> <td width="35"> <div align="center"> <? echo "$id"; ?></div></td><td width="129"> <? echo "$name"; ?></td><td width="184"> <? echo "$surname"; ?></td></tr><?$i++;}?> </table><?odbc_close($connect);}?></body></html>

  23. ** เพิ่มเติมครับหากต้องการค้นหานามสกุลด้วยให้เปลี่ยนเป็น $sql = "select * from customer Where name like '%$search_name%' or surname like '%ค่าหรือตัวแปรที่ต้องการค้นหา%'"; Out Put

  24. ตอนที่ 5 การแก้ข้อมูลในตาราง php4-6.php ฐานข้อมูล C:\Appserv\www\web\database.mdb ชื่อตาราง customer Form แก้ไข php4-7.php xxx xxx ดึงข้อมูลที่เลือกมาแก้ไข xxx บันทึกลงฐานข้อมูล php4-8.php

  25. php4-6.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้"); $sql = "select * from customer";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");?><table border="1" width="451"><tr> <td width="36"> <div align="center">id</div></td><td width="125"> <div align="center">name</div></td><td width="210"> <div align="center">surname</div></td><td width="52"> <div align="center">แก้ไข</div></td></tr><?

  26. php4-6.php ต่อ $i=1;while(odbc_fetch_row($execute,$i)){$id=odbc_result($execute,"id");$name=odbc_result($execute,"name");$surname=odbc_result($execute,"surname");?><table border="1" width="451"><tr> <td width="37"> <div align="center"> <? echo "$id"; ?></div></td><td width="122"> <? echo "$name"; ?></td><td width="212"> <? echo "$surname"; ?></td>

  27. php4-6.php ต่อ <td width="52"><div align="center"><a href=“php4-7.php?id=<?=$id;?>">แก้ไข</a></div></td></tr><?$i++;}?> </table><?odbc_close($connect);?> Out Put

  28. php4-7.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้"); $sql="SELECT * FROM customer Where id=$id"; $execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");$id=odbc_result($execute,"id");$name=odbc_result($execute,"name");$surname=odbc_result($execute,"surname");odbc_close($connect);?><html><body> <form name="form1" method="post" action=“php4-8.php">Name <input type="text" name="name" value="<?=$name;?>">Surname <input type="text" name="surname" value="<?=$surname;?>"><input type="submit" name="Submit" value="Update"><input type="hidden" name="id" value="<?=$id;?>"></form></body></html> Out put

  29. php4-8.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้");$sql = "update customer set name='$name',surname='$surname' where id=$id";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");odbc_close($connect);echo"ได้ทำการ แก้ไขข้อมูลเรียบร้อยแล้ว ";?> Out Put ได้ทำการ แก้ไขข้อมูลเรียบร้อยแล้ว

  30. ตอนที่ 6 การลบข้อมูลในตางราง ฐานข้อมูล C:\Appserv\www\web\database.mdb ชื่อตาราง customer

  31. php4-9.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้"); $sql = "select * from customer";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");?><table border="1" width="451"><tr> <td width="36"> <div align="center">id</div></td><td width="125"> <div align="center">name</div></td><td width="210"> <div align="center">surname</div></td><td width="52"> <div align="center">ลบ</div></td></tr></table><? $i=1;while(odbc_fetch_row($execute,$i)){$id=odbc_result($execute,"id");$name=odbc_result($execute,"name");$surname=odbc_result($execute,"surname");?>

  32. php4-9.php ต่อ <table border="1" width="451"><tr> <td width="37"> <div align="center"> <? echo "$id"; ?></div></td><td width="122"> <? echo "$name"; ?></td><td width="212"> <? echo "$surname"; ?></td><td width="52"><div align="center"><a href=“php4-10.php?id=<?=$id;?>">ลบ</a></div></td></tr></table><?$i++;}?><?odbc_close($connect);?> Out Put

  33. php4-10.php <?$dsn_name = "customer";$username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้");$sql = "delete from customer where id=$id";$execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้");odbc_close($connect);echo"ได้ทำการ ลบข้อมูลเรียบร้อยแล้ว ";?> Out Put ได้ทำการ ลบข้อมูลเรียบร้อยแล้ว

  34. ตัวอย่างการสร้างระบบ Login โดยใช้ฐานข้อมูล • ฐานข้อมูล C:\Appserv\www\web\database.mdb ตารางชื่อ Customer php4-11.php User : xxxx Password : ****** Login Cancel php4-12.php ยินดีต้อนรับคุณ..xx.สู่เมนูหลัก

  35. php4-11.php <FORM METHOD=POST ACTION="php4-12.php"> User: <INPUT TYPE="text" NAME=“user"><BR> Password:<INPUT TYPE="Password" NAME=“pass"><BR> <INPUT TYPE="submit" value="Login"> <INPUT TYPE="reset" value=“Cancel"> </FORM> Output User : xxxx Password : ****** Login Cancel

  36. <? $dsn_name = "customer"; $username =""; $password =""; $connect= odbc_connect($dsn_name, $username, $password) or die("ติดต่อ DSN ไม่ได้"); $sql="SELECT * FROM customer Where user='$user' and password='$pass'"; $execute = odbc_exec($connect, $sql) or die ("เอ็กซิคิวส์คำสั่งไม่ได้"); $id=odbc_result($execute,"id"); $name=odbc_result($execute,"name"); $surname=odbc_result($execute,"surname"); odbc_close($connect); ?> <html> <body> <?if(empty($id)) { echo " ไม่พบข้อมูล"; } else {?> ยินดีต้อนรับ <B>คุณ<?=$name;?><?=$surname;?></B>.. สู่เมนูหลัก <?}?></body> </html> php4-12.php ยินดีต้อนรับคุณ..xx.สู่เมนูหลัก Output

More Related