php与oracle连接函数详解oci_connect

来源:互联网 发布:hadoop2.7.1 windows 编辑:程序博客网 时间:2024/05/22 00:33

 此函数在php官方网站,我看了很多次,也研究了很多次,可是却一直没搞明白,一直到刚刚,写下来,做个笔记吧。

对于该函数我理解的也就是下面几种用法吧。我把代码从官网上帖过来,再加上自己理解的部分:

Examples

Example #1 Basic oci_connect() using Easy Connect syntax

<?php

// Connects to the XE service (i.e. database) on the "localhost" machine
$conn oci_connect('hr''welcome''localhost/XE');//这里XE其实就是你要访问的数据库主机名,开始我一直理解的是数据库名,所以这个函数我就不一直没搞明白
if (!
$conn) {
    
$e oci_error();
    
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid oci_parse($conn'SELECT * FROM employees');
oci_execute($stid);

echo 
"<table border='1'>\n";
while (
$row oci_fetch_array($stidOCI_ASSOC+OCI_RETURN_NULLS)) {
    echo 
"<tr>\n";
    foreach (
$row as $item) {
        echo 
"    <td>" . ($item !== null htmlentities($itemENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo 
"</tr>\n";
}
echo 
"</table>\n";

?>

Example #2 Basic oci_connect() using a Network Connect name

<?php

// Connects to the MYDB database described in tnsnames.ora file,
// One example tnsnames.ora entry for MYDB could be:
//   MYDB =
//     (DESCRIPTION =
//       (ADDRESS = (PROTOCOL = TCP)(HOST = mymachine.oracle.com)(PORT = 1521))
//       (CONNECT_DATA =
//         (SERVER = DEDICATED)
//         (SERVICE_NAME = XE)
//       )
//     )

$conn oci_connect('hr''welcome''MYDB');//MYDB这个名字就是在本机上安装配置oracle数据库客户端时,tnsnames.ora文件中,所配置的关于要访问的数据库的情况,就是此代码框中上面那些注释部分,所起的名字,这名字可是任何。
if (!$conn) {
    
$e oci_error();
    
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid oci_parse($conn'SELECT * FROM employees');
oci_execute($stid);

echo 
"<table border='1'>\n";
while (
$row oci_fetch_array($stidOCI_ASSOC+OCI_RETURN_NULLS)) {
    echo 
"<tr>\n";
    foreach (
$row as $item) {
        echo 
"    <td>" . ($item !== null htmlentities($itemENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo 
"</tr>\n";
}
echo 
"</table>\n";

?>

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");//这又是一种方式,这个在官方网站没有例子/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}/* Create table doesn't return a resultset */if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {    printf("Table myCity successfully created.\n");}/* Select queries return a resultset */if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {    printf("Select returned %d rows.\n", mysqli_num_rows($result));    /* free result set */    mysqli_free_result($result);}/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {    /* Note, that we can't execute any functions which interact with the       server until result set was closed. All calls will return an       'out of sync' error */    if (!mysqli_query($link, "SET @a:='this will not work'")) {        printf("Error: %s\n", mysqli_error($link));    }    mysqli_free_result($result);}mysqli_close($link);?>



其它的例子我还没搞明白,有明白的,可是交流一下。

以上代码地址:http://cn2.php.net/oci-connect

原创粉丝点击