aci_connect¶
aci_connect — 建立一个到 Oracle 服务器的连接
说明¶
aci_connect(
string $username,
string $password,
string $db = ?,
string $charset = ?,
int $session_mode = ?
): resource
aci_connect()返回一个大多数 ACI 调用都需要的连接标识符。session_mode 参数接受如下值:OCI_DEFAULT。
警告
注意: 对 aci_connect() 用同样的参数第二次及之后的调用将返回第一次调用所返回的连接句柄。 也就是说对一个句柄发出的查询也适用于另一个句柄,因为它们是同一个句柄。此行为演示于下面的例 1。 如果需要两个句柄在事务上互相隔离开,应该使用 aci_new_connect() 来替代。
使用客户端库来确定字符集。字符集不需要与数据库的字符集相匹配。如果不匹配,会尽可能地将数据从数据库字符集进行转换。因为依赖于字符集,可能不能给出可用的结果。转换也增加一些时间开销。
传递此参数可减少连接时间。
示例 #1 aci_connect() 例子
<?php
echo "<pre>";
$db = "";
$c1 = aci_connect("sysdba", "szoscar55", $db);
$c2 = aci_connect("sysdba", "szoscar55", $db);
function create_table($conn)
{
$stmt = aci_parse($conn, "create table scott.hallo (test varchar2(64))");
oci_execute($stmt);
echo $conn . " created table\n\n";
}
function drop_table($conn)
{
$stmt = aci_parse($conn, "drop table scott.hallo");
aci_execute($stmt);
echo $conn . " dropped table\n\n";
}
function insert_data($conn)
{
$stmt = aci_parse($conn, "insert into scott.hallo
values('$conn' || ' ' || to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");
aci_execute($stmt, OCI_DEFAULT);
echo $conn . " inserted hallo\n\n";
}
function delete_data($conn)
{
$stmt = aci_parse($conn, "delete from scott.hallo");
aci_execute($stmt, OCI_DEFAULT);
echo $conn . " deleted hallo\n\n";
}
function commit($conn)
{
aci_commit($conn);
echo $conn . " committed\n\n";
}
function rollback($conn)
{
aci_rollback($conn);
echo $conn . " rollback\n\n";
}
function select_data($conn)
{
$stmt = aci_parse($conn, "select * from scott.hallo");
aci_execute($stmt, OCI_DEFAULT);
echo $conn."----selecting\n\n";
while (oci_fetch($stmt)) {
echo $conn . " [" . oci_result($stmt, "TEST") . "]\n\n";
}
echo $conn . "----done\n\n";
}
create_table($c1);
insert_data($c1); // Insert a row using c1
insert_data($c2); // Insert a row using c2
select_data($c1); // Results of both inserts are returned
select_data($c2);
rollback($c1); // Rollback using c1
select_data($c1); // Both inserts have been rolled back
select_data($c2);
insert_data($c2); // Insert a row using c2
commit($c2); // Commit using c2
select_data($c1); // Result of c2 insert is returned
delete_data($c1); // Delete all rows in table using c1
select_data($c1); // No rows returned
select_data($c2); // No rows returned
commit($c1); // Commit using c1
select_data($c1); // No rows returned
select_data($c2); // No rows returned
drop_table($c1);
echo "</pre>";
?>
aci_connect() 如果出错则返回 false。
警告
注意: 在 PHP 5.0.0 之前的版本必须使用 acilogon() 替代本函数。该函数名仍然可用,为向下兼容作为 aci_connect() 的别名。不过其已被废弃,不推荐使用。
参见 aci_new_connect() 和 aci_close()。
参数¶
- username
- 用户名.
- password
- 用户密码.
connection_string 包含要连接的 数据库 实例。
如果不指定或者为 null,PHP 使用环境变量来确定连接的 数据库 实例。
character_set 使用 客户端库来确定字符集。字符集不需要与数据库的字符集相匹配。如果不匹配,会尽可能地将数据从数据库字符集进行转换。因为依赖于字符集,可能不能给出可用的结果。转换也增加一些时间开销。
如果不指定,客户端用LANG环境变量来决定字符集。
传递此参数可减少连接时间。
session_mode 此参数收受下列值:OCI_DEFAULT,OCI_SYSOPER 和 OCI_SYSDBA。
返回值¶
错误时返回连接标识符或false。
范例¶
示例 #2使用Easy connect语法的基本aci_connect()
<?php
// Connects to the OSRDB service on the "localhost" machine
$conn = aci_connect('sysdba', 'szoscar55', 'localhost:2003/OSRDB');
if (!$conn) {
$e = aci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = aci_parse($conn, 'SELECT * FROM employees');
aci_execute($stid);
echo "<table border='1'>\n";
while ($row = aci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
示例 #3 具有显式字符集的aci_connect()
<?php
$conn = aci_connect('sysdba', 'szoscar55', 'localhost:2003/OSRDB', 'UTF8');
if (!$conn) {
$e = aci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = aci_parse($conn, 'SELECT * FROM employees');
aci_execute($stid);
echo "<table border='1'>\n";
while ($row = aci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
示例 #4 使用对aci_connect()的多次调用
<?php
$c1 = aci_connect('sysdba', 'szoscar55', 'localhost:2003/OSRDB');
$c2 = aci_connect('sysdba', 'szoscar55', 'localhost:2003/OSRDB');
// Both $c1 and $c2 show the same PHP resource id meaning they use the
// same underlying database connection
echo "c1 is $c1<br>\n";
echo "c2 is $c2<br>\n";
function create_table($conn)
{
$stmt = aci_parse($conn, "create table hallo (test varchar2(64))");
aci_execute($stmt);
echo "Created table<br>\n";
}
function drop_table($conn)
{
$stmt = aci_parse($conn, "drop table hallo");
aci_execute($stmt);
echo "Dropped table<br>\n";
}
function insert_data($connname, $conn)
{
$stmt = aci_parse($conn, "insert into hallo
values(to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");
aci_execute($stmt, OCI_DEFAULT);
echo "$connname inserted row without committing<br>\n";
}
function rollback($connname, $conn)
{
aci_rollback($conn);
echo "$connname rollback<br>\n";
}
function select_data($connname, $conn)
{
$stmt = aci_parse($conn, "select * from hallo");
aci_execute($stmt, OCI_DEFAULT);
echo "$connname ----selecting<br>\n";
while (aci_fetch($stmt)) {
echo " " . aci_result($stmt, "TEST") . "<br>\n";
}
echo "$connname ----done<br>\n";
}
create_table($c1);
insert_data('c1', $c1); // Insert a row using c1
sleep(2); // sleep to show a different timestamp for the 2nd row
insert_data('c2', $c2); // Insert a row using c2
select_data('c1', $c1); // Results of both inserts are returned
select_data('c2', $c2); // Results of both inserts are returned
rollback('c1', $c1); // Rollback using c1
select_data('c1', $c1); // Both inserts have been rolled back
select_data('c2', $c2);
drop_table($c1);
// Closing one of the connections makes the PHP variable unusable, but
// the other could be used
aci_close($c1);
echo "c1 is $c1<br>\n";
echo "c2 is $c2<br>\n";
// Output is:
// c1 is Resource id #5
// c2 is Resource id #5
// Created table
// c1 inserted row without committing
// c2 inserted row without committing
// c1 ----selecting
// 09-DEC-09 12:14:43
// 09-DEC-09 12:14:45
// c1 ----done
// c2 ----selecting
// 09-DEC-09 12:14:43
// 09-DEC-09 12:14:45
// c2 ----done
// c1 rollback
// c1 ----selecting
// c1 ----done
// c2 ----selecting
// c2 ----done
// Dropped table
// c1 is
// c2 is Resource id #5
?>