PHP使用Oracle資料庫

來源:互聯網
上載者:User
oracle|資料|資料庫 【摘 要】 HP提供了2大類API(應用程式介面)來操作Oracle資料庫。一個是標準的Oracle處理函數(ORA) 另一個是Oracle 8調用介面函數(OCI8). 後者只能在Oracle 7或8版本上使用。 由於OCI8 提供了很多最佳化選項,因此只要有可能就應該採用 OCI8 介面。

在php3.0以上版本中,php內建了幾乎目前所有的資料庫處理函數,包括Oracle;在本文中我們通過一個執行個體來介紹了如何使用這些函數來操作Oracle資料庫。

PHP提供了2大類API(應用程式介面)來操作Oracle資料庫。一個是標準的Oracle處理函數(ORA) 另一個是Oracle 8調用介面函數(OCI8). 後者只能在Oracle 7或8版本上使用。 由於OCI8 提供了很多最佳化選項,因此只要有可能就應該採用 OCI8 介面。這裡我們分別用這二種函數集進行了示範。

首先本文的前提假設你已經裝好了 Oracle資料庫環境 和 PHP 開發環境.  如果不懂也沒多大關係,網上有很多相關的好文章可以參考。

第一步:建立 一個實驗用的資料庫

這個問題你可以請你的資料庫管理員或參考Oracle使用者手冊處理,這裡不再多講

用 ORA 建立資料表

即使你已經建立好了資料表,也請看看本段文字。它可以告訴你如何用PHP+SQL的技術操作Oracle

在本例中我們建立了一個用於存放個人email的資料表

相關PHP代碼:


PutEnv("ORACLE_SID=ORASID");
$connection = Ora_Logon ("username", "password");
if ($connection == false){
  echo Ora_ErrorCode($connection).": ".Ora_Error($connection)."";
  exit;
}  
$cursor = Ora_Open ($connection);
if ($cursor == false){
  echo Ora_ErrorCode($connection).": ".Ora_Error($connection)."";
  exit;
}  
$query = "create table email_info " .
       "(fullname varchar(255), email_address varchar(255))";
$result = Ora_Parse ($cursor, $query);
if ($result == false){
  echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)."";
  exit;
}
$result = Ora_Exec ($cursor);
if ($result == false){
  echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)."";
  exit;
}
Ora_Commit ($connection);
Ora_Close ($cursor);
Ora_Logoff ($connection);
?> 

為了處理Oracle資料庫,我們首先要和Oracle建立一個連接。
文法是 Ora_Logon (user, password),返回一個connectID.. 
提醒: 在此之前我們還必須設定環境變數: ORACLE_SID的值.

現在,我們可以通過該連接的 ID對Oracle進行互動式操作了。資料表名字就叫email_info吧。該表由2個域組成,一個儲存個人全名,(如:曉月)一個儲存email地址如(xiaoyue@163.net)

還需要一個遊標 Ora_Open.  該遊標常常用來枚舉資料。我們用 Ora_Parse 或 Ora_Exec 查詢Oracle的結果集.  Ora_Parse 效驗SQL文法正確性 而 Ora_Exec 則執行相應的SQL語句.  如果這一切都正常運行,那麼我們就運行 Ora_Commit來確認.

Create A Table Using OCI

下面我們將建立一個email個人資訊簿。這次採用OCI8 API指令

相關PHP代碼:


PutEnv("ORACLE_SID=ORASID");

$connection = OCILogon ("username", "password");
if ($connection == false){
   echo OCIError($connection)."
";
   exit;
}   

$query = "create table email_info " .
              "(fullname varchar(255), email_address varchar(255))";

$cursor = OCIParse ($connection, $query);
if ($cursor == false){
   echo OCIError($cursor)."
";
   exit;
}

$result = OCIExecute ($cursor);
if ($result == false){
   echo OCIError($cursor)."
";
   exit;
}

OCICommit ($connection);
OCILogoff ($connection);

?>

我們可以看到這2段代碼文法幾乎都一樣,區別僅僅函數名字不同; 其次,在OCI8中我們不需要專門運行開啟遊標的指令,在調用 OCIParse 系統就自動返回了一個遊標ID.

利用 ORA 向資料表 'email_info' 輸入資料

當使用者瀏覽這段指令碼時,顯示一個由姓名、email輸入欄位組成的表單;當使用者添好資料點擊提交時,指令碼程式將把這姓名、email儲存到'email_info'資料表中。

相關PHP代碼:


if ($submit == "click"){
  // The submit button was clicked!
  // Get the input for fullname and email then store it in the database.
  PutEnv("ORACLE_SID=ORASID");

  $connection = Ora_Logon ("username","password");
  if ($connection == false){
    echo Ora_ErrorCode($connection).": ".Ora_Error($connection)."
";
    exit;
  }

$cursor = Ora_Open ($connection);
if ($cursor == false){
  echo Ora_ErrorCode($connection).": ".Ora_Error($connection)."
";
  exit;
}   

  $query = "insert into email_info values ('$fullname', '$email')";
  $result = Ora_Parse ($cursor, $query);
  if ($result == false){
    echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)."
";
    exit;
  }

  $result = Ora_Exec ($cursor);
  if ($result == false){
    echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)."
";
     exit;
  }

  Ora_Commit ($connection);
  Ora_Close ($cursor);
  Ora_Logoff ($connection);
}
else{
  echo '
    

     <FORM action=insert.php method=post>

    請輸入姓名
    <INPUT name=fullname></INPUT>

    請輸入Email地址
    <INPUT name=email></INPUT>

    <INPUT name=submit type=submit value=click></INPUT>       

    </FORM>

    
  ';
}

?>

對了,這段指令碼必須存為insert.php,因為在調用的頁面中指定insert.php為表單處理程式

瀏覽效果:



請輸入姓名
請輸入Email地址


利用OCI向資料表 'email_info' 輸入資料

同上,只不過用OCI來寫

相關PHP代碼:


if ($submit == "click"){
  // The submit button was clicked!
  // Get the input for fullname and email then store it in the database.
  PutEnv("ORACLE_SID=ORASID");

  $connection = OCILogon ("username","password");
  if ($connection == false){
    echo OCIError($connection)."
";
    exit;
  }

  $query = "insert into email_info values ('$fullname', '$email')";
  $cursor = OCIParse ($connection, $query);
  if ($cursor == false){
    echo OCIError($cursor)."
";
    exit;
  }

  $result = OCIExecute ($cursor);
  if ($result == false){
    echo OCIError($cursor)."
";
    exit;
  }

  OCICommit ($connection);
  OCILogoff ($connection);
}
else{
  echo '
    

     <FORM action=insert.php method=post>

    請輸入姓名
    <INPUT name=fullname></INPUT>

    請輸入 Email 地址
    <INPUT name=email></INPUT>

    <INPUT name=submit type=submit value=click></INPUT>       

    </FORM>

    
  ';
}

?>

對了,這段指令碼必須存為insert.php,因為在調用的頁面中指定insert.php為表單處理程式

瀏覽效果:



請輸入姓名
請輸入Email地址

利用ORA列出全部資料表'email_info'中的資料


下面,我們將逐條讀出資料庫的內容,並以html表格形式顯示'email_info'資料表中的資料

相關PHP代碼:


PutEnv("ORACLE_SID=ORASID");

$connection = Ora_Logon ("username","password");
if ($connection == false){
  echo Ora_ErrorCode($connection).": ".Ora_Error($connection)."
";
  exit;
}

$cursor = Ora_Open ($connection);
if ($cursor == false){
  echo Ora_ErrorCode($connection).": ".Ora_Error($connection)."
";
  exit;
}   

$query = "select * from email_info";
$result = Ora_Parse ($cursor, $query);
if ($result == false){
  echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)."
";
  exit;
}

$result = Ora_Exec ($cursor);
if ($result == false){
  echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)."
";
  exit;
}

echo "











";echo " ";while (Ora_Fetch_Into ($cursor, &$values)){  $name = $values[0];  $email = $values[1];  echo " ";}echo "
Full Name Email Address
$name $email
";

Ora_Close ($cursor);
Ora_Logoff ($connection);

?>

程式啟動並執行瀏覽效果如下所示:

 
姓名 Email 地址
春花 springflower@163.com
秋月 autumnmoon@163.com
... ...
 

利用OCI列出全部資料表'email_info'中的資料


同上,只不過用OCI來寫

相關PHP代碼:


PutEnv("ORACLE_SID=ORASID");

$connection = OCILogon ("username","password");
if ($connection == false){
  echo OCIError($connection)."
";
  exit;
}

$query = "select * from email_info";
$cursor = OCIParse ($connection, $query);
if ($cursor == false){
  echo OCIError($cursor)."
";
  exit;
}

$result = OCIExecute ($cursor);
if ($result == false){
  echo OCIError($cursor)."
";
  exit;
}

echo "











";echo " ";while (OCIFetchInto ($cursor, $values)){  $name = $values[0];  $email = $values[1];  echo " ";}echo "
Full Name Email Address
$name $email
";

OCILogoff ($connection);

?>

程式啟動並執行瀏覽效果如下所示:

 
姓名 Email 地址
春花 springflower@163.com
秋月 autumnmoon@163.com
... ...
 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.