1 / 16

การใช้ PHP ติดต่อฐานข้อมูล

Internet for Education  . อาจารย์ช นิดา เรืองศิริวัฒน กุล หลักสูตรสาขาวิชาเทคโนโลยีสารสนเทศ. การใช้ PHP ติดต่อฐานข้อมูล. MySQL DATABASE FUNCTION.

Download Presentation

การใช้ PHP ติดต่อฐานข้อมูล

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. Internet for Education   อาจารย์ชนิดา เรืองศิริวัฒนกุล หลักสูตรสาขาวิชาเทคโนโลยีสารสนเทศ การใช้ PHPติดต่อฐานข้อมูล

  2. MySQL DATABASE FUNCTION MySQL เป็นระบบฐานข้อมูลที่ใช้งานร่วมกับ PHP หาใช้ได้ง่ายระบบมีประสิทธิ์ภาพสูงและเป็นแบบ RDBMS(ralational database management system)

  3. ระบบฐานข้อมูลที่ PHP สามารถเชื่อมต่อได้ Oracle Sybase mSQL MySQL Solid ODBC PostgreSQL Adabas D FilePro Velocis Informix dBase Unix dbm

  4. ฟังก์ชัน PHP สำหรับติดต่อฐานข้อมูล MySQL • mysql_connect()เชื่อมต่อเซิร์ฟเวอร์ฐานข้อมูล ซึ่งต้องใช้ ชื่อดาต้าเบสเซิร์ฟเวอร์, ชื่อผู้ใช้ และรหัสผ่าน • mysql_select_db()เลือกฐานข้อมูล • mysql_query()ส่งคำสั่ง sql ไปยังฐานข้อมูล • sql_fetch_array()ดึงข้อมูล

  5. mysql_connect • ฟังก์ชันในการเชื่อมต่อเซิร์ฟเวอร์ • รูปแบบ mysql_connect(ชื่อดาต้าเบส,ชื่อผู้ใช้,รหัสผ่าน); • ตัวอย่าง $host =“localhost”; $username=“root”; $password=“1234”; @mysql_connect($host,$username,$password) or die (“MySql connect fail”) ;

  6. mysql_connect • เครื่องหมาย @ หมายถึงให้ฟังก์ชันนี้แสดงข้อความแจ้งว่ามีการทำงานผิดพลาด • or die() หมายถึง หากเกิดปัญหาในการเชื่อมต่อ ให้พิมพ์ข้อความที่อยู่ในวงเล็บและออกจากสคริปต์โดยไม่มีการดำเนินการใด ๆ เพิ่มเติม

  7. mysql_select_db() • หลังจากเชื่อมต่อดาต้าเบสเซิร์ฟเวอร์แล้ว จะต้องทำการเลือกฐานข้อมูลใช้งาน • รูปแบบ mysql_select_db(ชื่อฐานข้อมูล); • ตัวอย่าง $db =“e_commerce”; @mysql_select_db($db) or die (“MySql select database fail”) ;

  8. mysql_query() • ฟังก์ชันส่งคำสั่ง SQL ไปที่ฐานข้อมูล • รูปแบบ mysql_query(คำสั่งภาษา SQL); • ตัวอย่าง $sql =“select * from quest order by ‘date’ DESC”; $result = mysql_query($sql) or die (mysql_error()) ; • ฟังก์ชันmysql_error() แสดงความผิดพลาดในการส่งคำสั่ง SQL เช่น เขียนผิดไวยากรณ์ของภาษา SQL หรือชื่อตาราง หรือชื่อฟิลด์ผิด

  9. mysql_fetch_array() • ฟังก์ชันการดึงข้อมูลทีละแถวของตาราง • รูปแบบ $ตัวแปร=mysql_fetch_array(ผลการดึงข้อมูล); • ตัวอย่าง $row=mysql_fetch_array($result); • ตัวแปร$rowเป็นอาร์เรย์ที่เก็บข้อมูล การแสดงข้อมูลในฟิลด์ “id” ให้เขียนว่า echo $row[“id”] ; • หรือถ้าต้องการดึงข้อมูลพร้อมกันหลายฟิลด์ echo “$row[id], $row[name], $row[date]”;

  10. mysql_num_rows () • ฟังก์ชันนับจำนวนแถวของข้อมูลในตาราง • รูปแบบ $ตัวแปร=mysql_num_rows(ผลการดึงข้อมูล); • ตัวอย่าง $num_rows = mysql_num_rows($result); • สามารถใช้ตัวแปร$num_rowsเป็นตัวแปรที่ใช้ตรวจสอบการดึงข้อมูลจากอาร์เรย์เพื่อใช้แสดงผลข้อมูลในตาราง

  11. mysql_num_rows () • ตัวอย่าง for($i=0; $i<$num_rows; $i++) { $row=mysql_fetch_array($result); echo "แสดงข้อมูล ".$row[“name"] ; echo "<br>" ; }

  12. การเขียนโปรแกรมแสดงผลการเขียนโปรแกรมแสดงผล • มีขั้นตอนดังนี้ • 1. เริ่มติดต่อฐานข้อมูล • 2. เขียนคำสั่ง SQL และ query ข้อมูล • 3. แสดงข้อมูล • 4. ปิดการติดต่อฐานข้อมูล

  13. ตัวอย่างการติดต่อฐานข้อมูลตัวอย่างการติดต่อฐานข้อมูล • <?php $host = “localhost”; // กำหนดชื่อ host $user = “”; // กำหนดชื่อล็อกอิน $pass = “”; // กำหนดรหัสผ่าน $db=“project”; // กำหนดชื่อดาต้าเบส @mysql_connect($host, $user, $pass) or die (“ติดต่อ serverไม่ได้”); @mysql_select_db($db) or die (“MySql select database fail”) ; $sql = “select * from user”; // กำหนดคำสั่ง SQL เพื่อแสดงข้อมูล $result = mysql_query($sql)or die (mysql_error()) ; ; //query คำสั่ง SQL $row=mysql_fetch_array($result); $num_rows = mysql_num_rows($result); // ตัวแปรจำนวนเรคคอร์ด echo “แสดงชื่อสมาชิก” . $row[“name”] ; echo “ตาราง user มีจำนวนเรคคอร์ด = ”. $num_rows; ?>

  14. Case Study1 : Member

  15. ออกแบบฐานข้อมูล • ตาราง member • username ชื่อเข้าใช้ของสมาชิก • password รหัสผ่านของสมาชิก • name ชื่อ-สกุลสมาชิก • address ที่อยู่ • tel เบอร์โทรศัพท์ • email อีเมล์ของสมาชิก • comment ข้อเสนอแนะ

  16. Member ประกอบด้วยไฟล์อะไรบ้าง • login.php แสดงแบบฟอร์มการล็อกอิน • checklogin.php ตรวจสอบผลการล็อกอิน • register.html แสดงฟอร์มการสมัครสมาชิกใหม่ • checkregister.php รายงานผลการสมัครสมาชิกใหม่

More Related