`
streamsong
  • 浏览: 80463 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

php连接oracle

阅读更多
windows和Linux都能执行

Oracle Call Interface(OCI)使用户可以访问 Oracle 10,Oracle9,Oracle8 和 Oracle7 数据库。支持将 PHP 变量与 Oracle 占位符(placeholder)绑定,具有完整的 LOB,FILE 和 ROWID 支持,以及允许使用用户提供的定义变量。

例子 1. 基本查询

<?php

$conn = oci_connect('hr', 'hr', 'orcl');
if (!$conn) {
  $e = oci_error();
  print htmlentities($e['message']);
  exit;
}

$query = 'SELECT * FROM DEPARTMENTS';

$stid = oci_parse($conn, $query);
if (!$stid) {
  $e = oci_error($conn);
  print htmlentities($e['message']);
  exit;
}

$r = oci_execute($stid, OCI_DEFAULT);
if(!$r) {
  $e = oci_error($stid);
  echo htmlentities($e['message']);
  exit;
}

print '<table border="1">';
while($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
  print '<tr>';
     foreach($row as $item) {
       print '<td>'.($item?htmlentities($item):' ').'</td>';
     }
     print '</tr>';
}
print '</table>';

oci_close($conn);
?> 



例子 2. 用绑定变量插入

<?php

// Before running, create the table:
//   CREATE TABLE MYTABLE (mid NUMBER, myd VARCHAR2(20));

$conn = oci_connect('scott', 'tiger', 'orcl');

$query = 'INSERT INTO MYTABLE VALUES(:myid, :mydata)';

$stid = oci_parse($conn, $query);

$id = 60;
$data = 'Some data';

oci_bind_by_name($stid, ':myid', $id);
oci_bind_by_name($stid, ':mydata', $data);

$r = oci_execute($stid);

if($r)
  print "One row inserted";

oci_close($conn);

?> 



例子 3. 将数据插入到 CLOB 列中

<?php

// Before running, create the table:
//     CREATE TABLE MYTABLE (mykey NUMBER, myclob CLOB);

$conn = oci_connect('scott', 'tiger', 'orcl');

$mykey = 12343;  // arbitrary key for this example;

$sql = "INSERT INTO mytable (mykey, myclob)
        VALUES (:mykey, EMPTY_CLOB())
        RETURNING myclob INTO :myclob";

$stid = oci_parse($conn, $sql);
$clob = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($stid, ":mykey", $mykey, 5);
oci_bind_by_name($stid, ":myclob", $clob, -1, OCI_B_CLOB);
oci_execute($stid, OCI_DEFAULT);
$clob->save("A very long string");

oci_commit($conn);

// Fetching CLOB data

$query = 'SELECT myclob FROM mytable WHERE mykey = :mykey';

$stid = oci_parse ($conn, $query);
oci_bind_by_name($stid, ":mykey", $mykey, 5);
oci_execute($stid, OCI_DEFAULT);

print '<table border="1">';
while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
  $result = $row['MYCLOB']->load();
  print '<tr><td>'.$result.'</td></tr>';
}
print '</table>';

?> 



可以很容易地访问存储过程,就和从命令行访问一样。 例子 4. 使用存储过程

<?php
// by webmaster at remoterealty dot com
$sth = oci_parse($dbh, "begin sp_newaddress( :address_id, '$firstname',
'$lastname', '$company', '$address1', '$address2', '$city', '$state',
'$postalcode', '$country', :error_code );end;");

// This calls stored procedure sp_newaddress, with :address_id being an
// in/out variable and :error_code being an out variable.
// Then you do the binding:

   oci_bind_by_name($sth, ":address_id", $addr_id, 10);
   oci_bind_by_name($sth, ":error_code", $errorcode, 10);
   oci_execute($sth);

?> 



连接处理
OCI8 扩展提供了 3 个不同函数来连接 Oracle。取决于用户来使用对自己的应用程序最合适的函数。本节的信息有助于用户作出合适的选择。

连接到 Oracle 服务器从所需的时间上来讲是个相当花费的操作。oci_pconnect() 函数使用了一个连接的持久缓冲区,可以被不同的脚本请求重复使用。这意味着通常在每个 PHP 进程(或 Apache 子进程)中只需要连接一次。

如果应用程序连接 Oracle 时对每个 web 用户都使用了不同的认证信息,则由 oci_pconnect() 使用的持久缓冲区就用处不大了,因为随着并发用户的增加,到某个程度后会由于要保持太多的空闲连接而对 Oracle 服务器的整体性能起到逆反的影响。如果应用程序是这样的架构,建议要么用 oci8.max_persistent 和 oci8.persistent_timeout 配置选项(此二者可以使用户控制持久连接缓冲区的大小和生命周期)来协调应用程序,要么用 oci_connect() 来连接。

oci_connect() 和 oci_pconnect() 都使用了一个连接缓冲区。如果在某个脚本中用同样的参数多次调用 oci_connect(),则第二个和之后的调用会返回已有的连接句柄。oci_connect() 使用的连接缓冲区会在脚本执行完毕后或者明确地关闭了连接句柄时被清空。oci_pconnect() 有相似的行为,不过其缓冲区独立地维持着并在不同请求之间都存活着。

要记住此缓冲特性,因为它使两个句柄没有在事务级隔离开来(事实上是同一个连接句柄,因此没有任何方式的隔离)。如果应用程序需要两个独立的,事务级隔离的连接,应该使用 oci_new_connect()。

oci_new_connect() 总是创建一个到 Oracle 服务器的新连接,不管其它连接是否已经存在。高流量的 web 应用应该避免使用 oci_new_connect(),尤其是在程序最忙的部分。




有关于它的其他函数:
目录
OCI-Collection->append -- 向 collection 增加单元
OCI-Collection->assign -- 从现有的另一个 collection 向 collection 赋值
OCI-Collection->assignElem -- 给 collection 中的单元赋值
OCI-Collection->free -- 释放关联于 collection 的对象的资源
OCI-Collection->getElem -- 返回单元的值
OCI-Collection->max -- 返回 collection 中单元的最大数目
OCI-Collection->size -- 返回 collection 中的单元数目
OCI-Collection->trim -- 从 collection 尾端开始删除单元
OCI-Lob->append -- Appends data from the large object to another large object
OCI-Lob->close -- 关闭 LOB 描述符
OCI-Lob->eof -- Tests for end-of-file on a large object's descriptor
OCI-Lob->erase -- Erases a specified portion of the internal LOB data
OCI-Lob->export -- 将 LOB 的内容导出到文件中
OCI-Lob->flush -- Flushes/writes buffer of the LOB to the server
OCI-Lob->free -- 释放与 LOB 描述符所关联的资源
OCI-Lob->getBuffering -- Returns current state of buffering for the large object
OCI-Lob->import -- 将数据从文件导入 LOB
OCI-Lob->load -- 返回大对象的内容
OCI-Lob->read -- Reads part of the large object
OCI-Lob->rewind -- Moves the internal pointer to the beginning of the large object
OCI-Lob->save -- 将数据保存到大对象中
OCI-Lob->seek -- Sets the internal pointer of the large object
OCI-Lob->setBuffering -- Changes current state of buffering for the large object
OCI-Lob->size -- Returns size of large object
OCI-Lob->tell -- Returns current position of internal pointer of large object
OCI-Lob->truncate -- Truncates large object
OCI-Lob->write -- Writes data to the large object
OCI-Lob->writeTemporary -- 写入一个临时的大对象
oci_bind_by_name -- 绑定一个 PHP 变量到一个 Oracle 位置标志符
oci_cancel -- 取消从游标读取数据
oci_close -- 关闭 Oracle 连接
oci_commit -- 提交未执行的事务处理
oci_connect -- 建立一个到 Oracle 服务器的连接
oci_define_by_name -- 在 SELECT 中使用 PHP 变量作为定义的步骤
oci_error -- 返回上一个错误
oci_execute -- 执行一条语句
oci_fetch_all -- 获取结果数据的所有行到一个数组
oci_fetch_array -- Returns the next row from the result data as an associative or numeric array, or both
oci_fetch_assoc -- Returns the next row from the result data as an associative array
oci_fetch_object -- Returns the next row from the result data as an object
oci_fetch_row -- Returns the next row from the result data as a numeric array
oci_fetch -- Fetches the next row into result-buffer
oci_field_is_null -- 检查字段是否为 NULL
oci_field_name -- 返回字段名
oci_field_precision -- 返回字段精度
oci_field_scale -- 返回字段范围
oci_field_size -- 返回字段大小
oci_field_type_raw -- 返回字段的原始 Oracle 数据类型
oci_field_type -- 返回字段的数据类型
oci_free_statement -- 释放关联于语句或游标的所有资源
oci_internal_debug -- 打开或关闭内部调试输出
oci_lob_copy -- Copies large object
oci_lob_is_equal -- Compares two LOB/FILE locators for equality
oci_new_collection -- 分配新的 collection 对象
oci_new_connect -- 建定一个到 Oracle 服务器的新连接
oci_new_cursor -- 分配并返回一个新的游标(语句句柄)
oci_new_descriptor -- 初始化一个新的空 LOB 或 FILE 描述符
oci_num_fields -- 返回结果列的数目
oci_num_rows -- 返回语句执行后受影响的行数
oci_parse -- 配置 Oracle 语句预备执行
oci_password_change -- 修改 Oracle 用户的密码
oci_pconnect -- 使用一个持久连接连到 Oracle 数据库
oci_result -- 返回所取得行中字段的值
oci_rollback -- 回滚未提交的事务
oci_server_version -- 返回服务器版本信息
oci_set_prefetch -- 设置预提取行数
oci_statement_type -- 返回 OCI 语句的类型
ocibindbyname -- oci_bind_by_name() 的别名
ocicancel -- oci_cancel() 的别名
ocicloselob -- OCI-Lob->close 的别名
ocicollappend -- OCI-Collection->append 的别名
ocicollassign -- OCI-Collection->assign 的别名
ocicollassignelem -- OCI-Collection->assignElem 的别名
ocicollgetelem -- OCI-Collection->getElem 的别名
ocicollmax -- OCI-Collection->max 的别名
ocicollsize -- OCI-Collection->size 的别名
ocicolltrim -- OCI-Collection->trim 的别名
ocicolumnisnull -- oci_field_is_null() 的别名
ocicolumnname -- oci_field_name() 的别名
ocicolumnprecision -- oci_field_precision() 的别名
ocicolumnscale -- oci_field_scale() 的别名
ocicolumnsize -- oci_field_size() 的别名
ocicolumntype -- oci_field_type() 的别名
ocicolumntyperaw -- oci_field_type_raw() 的别名
ocicommit -- oci_commit() 的别名
ocidefinebyname -- oci_define_by_name() 的别名
ocierror -- oci_error() 的别名
ociexecute -- oci_execute() 的别名
ocifetch -- oci_fetch() 的别名
ocifetchinto -- 获取下一行到一个数组
ocifetchistatement -- oci_fetch_all() 的别名
ocifreecollection -- OCI-Collection->free 的别名
ocifreecursor -- oci_free_statement() 的别名
ocifreedesc -- OCI-Lob->free 的别名
ocifreestatement -- oci_free_statement() 的别名
ociinternaldebug -- oci_internal_debug() 的别名
ociloadlob -- OCI-Lob->load 的别名
ocilogoff -- oci_close() 的别名
ocilogon -- oci_connect() 的别名
ocinewcollection -- oci_new_collection() 的别名
ocinewcursor -- oci_new_cursor() 的别名
ocinewscriptor -- oci_new_descriptor() 的别名
ocinlogon -- oci_new_connect() 的别名
ocinumcols -- oci_num_fields() 的别名
ociparse -- oci_parse() 的别名
ociplogon -- oci_pconnect() 的别名
ociresult -- oci_result() 的别名
ocirollback -- oci_rollback() 别名
ocirowcount -- oci_num_rows() 的别名
ocisavelob -- OCI-Lob->save 的别名
ocisavelobfile -- OCI-Lob->import 的别名
ociserverversion -- oci_server_version() 的别名
ocisetprefetch -- oci_set_prefetch() 的别名
ocistatementtype -- oci_statement_type() 的别名
ociwritelobtofile -- OCI-Lob->export 的别名
ociwritetemporarylob -- OCI-Lob->writeTemporary 的别名

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics