Exzilla.net
Contact
Home -> Articles -> How to access Oracle Database Server from PHP
 
Features
Forums
Files
Blogs
Document ID: # Howto-orcl-PHP-01 August 9, 2004
Document Title : How to access Oracle Database Server from PHP

Document Details

การติดต่อใช้งาน Oracle Database ด้วย PHP

บทความนี้เป็นตัวอย่างง่ายๆ ที่ช่วยให้ท่านเข้าใจในการใช้้ PHP ติดต่อทำงานร่วมกับ Oracle database แนะนำว่า ผู้อ่านควรมีความรู้เบื้องต้นทางด้าน PHP programming language ก่อนนะครับ (แต่ถ้ายังไม่คุ้นเคยเท่าไร แนะนำให้ดู References ให้ส่วนท้ายของเอกสารนี้นะครับ)

เรามาเริ่ม PHP script กันด้วย HTML code

<HTML>
<HEAD><TITLE> Simple PHP/Oracle Query Example </TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<B>Employees</B>
<BR><BR>

และในขั้นตอนต่อมา ก็เป็นการเริ่มต้น PHP code ซึ่งเป็นการสร้าง connection ในการติดต่อกับ Oracle Database

<?php $connection = OCILogon("scott", 
                "tiger", "Fuju"); ?> 

จากตัวอย่าง จะเป็นการบอกว่า เราต้องการติดต่อกับ Database โดยผ่าน Connection String ชื่อ fuju ( รายละเอียด connection string details ที่นี่) และใช้ User ชื่อ scott ในการติดต่อซึ่งมี Password เป็น Tiger ( ดูรายละเอียด เรื่อง Database User ได้ ที่นี่) หลังจากได้สร้าง Connection เรียบร้อยแล้ว ก็ถึงเวลาสร้าง Statement และทำการ execute ซึ่ง Statement
ก็จะเป็นคำสั่ง SQL ที่ใช้ในการดึงข้อมูล (ดูรายละเอียดการสร้าง Table และการใช้ งาน SQL เบื้องต้นได้ ที่นี่)

<?php $stmt = OCIParse($connection, 
                "SELECT * FROM scott.Employees"); OCIExecute($stmt); ?>

ตัวอย่าง Code ในเบื้องต้น ยังเป็นการดึงข้อมูลในทุกๆ Row ที่อยู่ใน Table Employees ที่อยู่ภายใต้ Schema ของ Scott ออกมา ซึ่งก็จะมี Column ชื่อ EMPID, ENAME, ESURNAME, SALARY, STARTDATE ตามลำดับ

และเมื่อ statement นั้นได้ถูก executed แล้ว เราก็สามารถอ่านค่าจาก Result Set เพื่อทำการแสดงผลได้ดังนี้

<?php
   // Start of table and column headings (ID and Name)
   print "<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\">\n";
   print " <TR><TH>ID</TH><TH>Name</TH><TH>SURNAME</TH>\n";
print " <TH>SALARY</TH><TH>STARTDATE</TH></TR>\n"; // Loop through results while(OCIFetch($stmt)) { print " <TR>\n"; print " <TD>" . OCIResult($stmt, "EMPID") . "</TD>\n"; print " <TD>" . OCIResult($stmt, "ENAME") . "</TD>\n"; print " <TD>" . OCIResult($stmt, "ESURNAME") . "</TD>\n"; print " <TD>" . OCIResult($stmt, "SALARY") . "</TD>\n"; print " <TD>" . OCIResult($stmt, "STARTDATE") . "</TD>\n"; print " </TR>\n"; } print "\n"; // As always, free the resources. OCIFreeStatement($stmt); OCILogoff($connection); ?>

แต่ละบรรทัดในการเสดงผล จะมีการใช้ Function OCIResult เพื่ออ่านค่าแต่ละ COLUMN จากข้อมูลของแต่ละ Row ที่ได้มา (แม้ว่าชื่อ COLUMN สำหรับ SQL นั้นไม่เป็น case-sensitive แต่ใน PHP สนใจเรื่อง case-sensitive นะครับ)
และในส่วนสุดท้าย ก็ต้องมีการ Disconnect จาก Database Server เพื่อทำการคืน Resource ด้วย

และก่อนจบ PHP Script ก็จะเป็นส่วนปิดของ html ดังนี้ครับ

</CENTER>
</BODY>
</HTML>

ตัวอย่าง Source Code ที่สมบรูณ์ กดที่นี่

ข้อมูลเพิ่มเติม

Recommended Website

Magazine and Online Learning

e-books

Books (You can download Oracle books from http://technet.oracle.com)

    • Oracle Concepts, 1999, Oracle Corporation.
    • SQL*PLUS user's Guide and Reference, 1999, Oracle Corporation
    • Oracle SQL*Plus: The Definitive Guide - A good reference for learning how to use SQL*Plus. http://www.oreilly.com/catalog/orsqplus/

See Also:

 



Copyright (c) 2001-2009 - Exzilla.net -  All Rights Reserved.
Contact Us | Privacy Policy | Terms & Conditions