简体   繁体   English

如何从PHP连接Oracle数据库

[英]How to connect an Oracle database from PHP

如何从PHP连接到Oracle数据库?

Forth link in google after searching for your exact questions brought up the following link: http://me2learn.wordpress.com/2008/10/18/connect-php-with-oracle-database/ 在搜索了您的确切问题后,谷歌中的链接提出了以下链接: http//me2learn.wordpress.com/2008/10/18/connect-php-with-oracle-database/

<?php
    $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.34)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;

    if($c = OCILogon("system", "your database password", $db))
    {
        echo "Successfully connected to Oracle.\n";
        OCILogoff($c);
    }
    else
    {
        $err = OCIError();
        echo "Connection failed." . $err[text];
    }
?>

I assumed you want to connect php with oracle databases. 我以为你想用php连接oracle数据库。 so, I give you two of file, this is represent a basic php-oracle for your reference. 所以,我给你两个文件,这是一个基本的php-oracle供你参考。 good luck! 祝好运!

form.php form.php的

<form name="form1" method="post" action="login.php">
  <label> User Name
  <input type="text" name="nis" id="nis">
  </label>
  <label> Password
  <input type="password" name="password" id="password">
  </label>
  <label>
  <input type="submit" name="submit" id="button" value="Login">
  </label>
</form>

login.php 的login.php

<?php

//create table users (userid varchar2(10), password varchar2(20), constraint pk_users primary key (userid));
//insert into users values('kharis', 'pass123');

$nis = isset($_POST['nis']) == true ? $_POST['nis'] : '';
$password= isset($_POST['password']) == true ? $_POST['password'] : '';

if(empty($nis) or empty($password)){
    echo "UserID atau Password kosong";}
else
{
    $db = "(DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = patronus.ad-ins.com)(PORT = 1521))
        (CONNECT_DATA =
          (SERVER = DEDICATED)
          (SERVICE_NAME = XE)
        )
      )" ;
    $connect = oci_connect("HR", "hr", "XE");
    $query = "SELECT * from users WHERE userid='".$nis."' and password='".$password."'";
    $result = oci_parse($connect, $query);
    oci_execute($result);
    $tmpcount = oci_fetch($result);
    if ($tmpcount==1) {
        echo "Login Success";}
    else
    {
        echo "Login Failed";
    }
}
?>

PHP provides Oracle OCI8 functions . PHP提供Oracle OCI8功能 Other options are PDO with the Oracle Driver and (if oracle supports it) ODBC . 其他选项是带有Oracle驱动程序的 PDO和(如果oracle支持它) ODBC

I have created a connection in easiest way as follows: 我以最简单的方式创建了一个连接,如下所示:

Step 1. Determine what PHP bit version (32-bit or 64-bit) you are running. 步骤1.确定您正在运行的PHP位版本(32位或64位)。 If PHP_INT_SIZE is value of 4, then version is 32-bit. 如果PHP_INT_SIZE的值为4,则版本为32位。 If PHP_INT_SIZE is value of 8, then version is 64-bit. 如果PHP_INT_SIZE的值为8,则版本为64位。 Use the following code below: 使用以下代码:

<?php
    switch(PHP_INT_SIZE) {
        case 4:
            echo '32-bit version of PHP';
            break;
        case 8:
            echo '64-bit version of PHP';
            break;
        default:
            echo 'PHP_INT_SIZE is ' . PHP_INT_SIZE;
    } 
?>

Step 2. Download the "InstantClient Package - Basic" for Windows from the [OTN InstantClient page][1]. 步骤2.从[OTN InstantClient页面] [1]下载Windows的“InstantClient Package - Basic”。 Make sure you download the corresponding bit-version from step 1. 确保从步骤1下载相应的位版本。

Step 3. Unzip the InstantClient files to C:\\instantclient_11_2 and edit the Windows PATH environment to include C:\\instantclient_11_2 . 步骤3.将InstantClient文件解压缩到C:\\instantclient_11_2并编辑Windows PATH环境以包含C:\\instantclient_11_2 For example, on Windows XP, follow `Start > Control Panel > System > 例如,在Windows XP上,请按照“开始”>“控制面板”>“系统”>

Advanced > Environment Variables` and edit `PATH` in the `System`
> variables list.
 Step 3. In your `php.ini` file, enable the following lines: 
extension=php_oci8_11g.dll
extension=php_openssl.dll

Finally, restart your Apache server.

http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

Its unclear from your question if you want to configure your PHP install to connect to oracle or if you just want the php syntax to make the connection? 从您的问题中不清楚您是否要配置PHP安装以连接到oracle,或者您只是想要php语法来建立连接?

As there is already a Windows specific answer and the connection systax here, these links should point you in the right direction for a linux based install of the Oracle client libraries. 由于这里已经有一个Windows特定的答案和连接systax,这些链接应指向正确的方向,以便安装基于Linux的Oracle客户端库。

The basic steps are.. 基本步骤是......

  • uncompress the Oracle Client Libraries (Basic, SDK and sqlplus packages) 解压缩Oracle客户端库(Basic,SDK和sqlplus包)
  • add LD_LIBRARY_PATH to your environment 将LD_LIBRARY_PATH添加到您的环境中
  • fix some symlinks for the libraries 修复库的一些符号链接
  • install PECL OCI8, this compiles the shared object so your system will need a C compiler 安装PECL OCI8,这会编译共享对象,因此您的系统需要一个C编译器
  • enable the extension in the php.ini file. 在php.ini文件中启用扩展。 Remember your system might have separate php.ini files for CLI and webserver so check for both. 请记住,您的系统可能有单独的CLI和Web服务器的php.ini文件,因此请检查两者。
  • restart your webserver 重启你的网络服务器

These steps are on multiple blog posts so I won't reinvent the wheel here. 这些步骤在多个博客文章中,所以我不会在这里重新发明轮子。 but here are some links that look pretty good 但这里有一些看起来很不错的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM