如何从 PHP 连接 Oracle 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5947809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect an Oracle database from PHP
提问by Tharindu
How do I connect to an Oracle database from PHP?
如何从 PHP 连接到 Oracle 数据库?
回答by RobertPitt
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];
}
?>
回答by Kharis
I assumed you want to connect php with oracle databases. so, I give you two of file, this is represent a basic php-oracle for your reference. good luck!
我假设您想将 php 与 oracle 数据库连接。所以,我给你两个文件,这是一个基本的 php-oracle 供你参考。祝你好运!
form.php
表单.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
登录.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";
}
}
?>
回答by Oswald
PHP provides Oracle OCI8 functions. Other options are PDO with the Oracle Driverand (if oracle supports it) ODBC.
PHP 提供了Oracle OCI8 函数。其他选项是带有Oracle 驱动程序的PDO和(如果 oracle 支持)ODBC。
回答by Pankaj katiyar
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. If PHP_INT_SIZE is value of 4, then version is 32-bit. If PHP_INT_SIZE is value of 8, then version is 64-bit. Use the following code below:
我以最简单的方式创建了一个连接,如下所示:
步骤 1. 确定您正在运行的 PHP 位版本(32 位或 64 位)。如果 PHP_INT_SIZE 的值为 4,则版本为 32 位。如果 PHP_INT_SIZE 的值为 8,则版本为 64 位。使用以下代码:
<?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]. Make sure you download the corresponding bit-version from step 1.
Step 3. Unzip the InstantClient files to
C:\instantclient_11_2
and edit the Windows PATH environment to includeC:\instantclient_11_2
. For example, on Windows XP, follow `Start > Control Panel > System >
步骤 2. 从 [OTN InstantClient 页面][1] 下载适用于 Windows 的“InstantClient Package - Basic”。确保从步骤 1 下载相应的位版本。
步骤 3. 将 InstantClient 文件解压缩到
C:\instantclient_11_2
并编辑 Windows PATH 环境以包含C:\instantclient_11_2
. 例如,在 Windows XP 上,按照“开始 > 控制面板 > 系统 >
Advanced > Environment Variables` and edit `PATH` in the `System`
> variables list.
Step 3. In your `php.ini` file, enable the following lines:
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
回答by nick fox
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 特定的答案和连接系统税,这些链接应该为您指明正确的方向,用于基于 linux 的 Oracle 客户端库安装。
The basic steps are..
基本步骤是..
- uncompress the Oracle Client Libraries (Basic, SDK and sqlplus packages)
- add LD_LIBRARY_PATH to your environment
- fix some symlinks for the libraries
- install PECL OCI8, this compiles the shared object so your system will need a C compiler
- enable the extension in the php.ini file. Remember your system might have separate php.ini files for CLI and webserver so check for both.
- restart your webserver
- 解压缩 Oracle 客户端库(Basic、SDK 和 sqlplus 包)
- 将 LD_LIBRARY_PATH 添加到您的环境中
- 修复库的一些符号链接
- 安装 PECL OCI8,这将编译共享对象,因此您的系统将需要一个 C 编译器
- 在 php.ini 文件中启用扩展。请记住,您的系统可能有单独的 php.ini 文件用于 CLI 和网络服务器,因此请检查两者。
- 重新启动您的网络服务器
These steps are on multiple blog posts so I won't reinvent the wheel here. but here are some links that look pretty good
这些步骤在多篇博客文章中,所以我不会在这里重新发明轮子。但这里有一些看起来不错的链接