How do I connect to an Oracle database from PHP?
-
What are you looking for, connection string? Take a look at PDO or OCI_ at php.net/manual/en/intro.oci8.phpMichael J.V.– Michael J.V.2011-05-10 09:14:39 +00:00Commented May 10, 2011 at 9:14
-
One important thing: If you want to connect to a remote Oracle server then you need to install the Oracle Instant Client. More information and a link to OIC here: de.php.net/manual/en/oci8.requirements.phpRaffael Luthiger– Raffael Luthiger2011-05-10 09:47:08 +00:00Commented May 10, 2011 at 9:47
6 Answers
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/
<?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];
}
?>
1 Comment
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!
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
<?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";
}
}
?>
Comments
PHP provides Oracle OCI8 functions. Other options are PDO with the Oracle Driver and (if oracle supports it) ODBC.
Comments
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:
<?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_2and edit the Windows PATH environment to includeC:\instantclient_11_2. For example, on Windows XP, follow `Start > Control Panel > System >
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
Comments
PHP has several extensions that let applications use Oracle Database. The PHP extensions that connect to Oracle Database are written in C and linked into the PHP binary. The extensions are:
- OCI8
- PDO_OCI driver for PDO
You can also use the ODBC extension;
Using PHP OCI8 Extension:-
<?php
// Create connection to Oracle
$conn = oci_connect("phphol", "welcome", "//localhost/orcl");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
?>
The oci_connect() function contains the username as phphol, the password as welcome, and the connection string as //localhost/orcl. In this case, Oracle's Easy Connect connection string syntax is used. It consists of the hostname and the DB service name.
The oci_close() function closes the connection. Any standard connections not explicitly closed will be automatically released when the script ends.
Using PHP PDO Extension:-
<?php
$dbh = new PDO('oci:dbname=localhost/XE', 'hr', 'welcome');
$s = $dbh->prepare("select city from locations");
$s->execute();
while (($r = $s->fetch(PDO::FETCH_ASSOC)) != false) {
echo htmlentities($r['CITY']) . "<br>";
}
?>
More details can be found below references:-
Comments
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?
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.
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
These steps are on multiple blog posts so I won't reinvent the wheel here. but here are some links that look pretty good