Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 442 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Connecting to a remote database using PHP

Hello experts,
I've looked into many options so far and haven't come very far in my endeavor. I'm building a web based dashboard for a company I am contracted to and I'm having a very difficult time connecting to an oracle database from my dashboard. I'm attempting to use PHP to create a connection to the database. Then I'd like to query the database and present the results on the dashboard. The database is password protected and likewise the server hosting the database is also password protected. I'm not finding much help in other places I've looked and this is the only bump in the road so far for my dashboard build.
I was told to look into using the database URI but I wasn't sure if that would work since the database is not on the web server that the dashboard will be hosted on. I've also looked into using the PDO::__construct() method but its unclear what names and information I should be using for parameters.
I'm really needing some help on this and any help or advice is appreciated.
Answers
-
As a start you could try something like the following. The DBA should be able to give you the connect string or service name & credentials. You can check the PHP manual for examples, or look at the PDO_OCI test suite.
<?php try { $dbh = new PDO('oci:dbname=localhost/XE', 'hr', 'welcome'); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); exit; } $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); try { $s = $dbh->prepare("select city from locations"); $s->execute(); while ($r = $s->fetch(PDO::FETCH_ASSOC)) { echo $r['CITY'] . "<br>"; } } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); exit; } ?>
However my general recommendation is to use OCI8 instead of PDO_OCI, since OCI8 has better features & scalability. There is a discussion on connect strings in http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html that also applies to PDO_OCI, you just to use the additional "oci:dbname=" prefix for PDO_OCI
-
Yeah, I'd rather use the oci_connect too. I installed the Oracle Instant Client and SQL*Plus. I also created a specific tnsnames.ora file just for my test dashboard (I'm creating the dashboard and testing it from my own local machine and when it is finished being built I will move it to our company's web server). When I use the sqlplus command from a command prompt it gives errors. I'm wondering if the listener on the remote database's host server needs to be configured to accept remote connections from my local machine. This is from the command prompt:
SQL*Plus: Release 11.2.0.2.0 Production on Mon Jun 22 08:45:43 2015
Copyright (c) 1982, 2010, Oracle. All rights reserved.
ERROR:
ORA-12545: Connect failed because target host or object does not exist
Enter user-name: user
Enter password:
ERROR:
ORA-12560: TNS:protocol adapter error
Not sure what is causing this. Also if I were to use a connection string, how do I type that into my PHP.
-
So I looked into the error logs and they are saying:
Fatal error: Call to undefined function oci_connect() in C:\inetpub\wwwroot\testconnection.php on line 8
I've included the oci.dll, ociw32.dll, and the php_oci8_12c.dll files in the php.ini and still same error.
-
Check out the steps in http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html
It looks like you don't have Oracle libraries in PATH.
-
I appreciate your time, but with all the problems we were having my team decided we were going to slash the PHP idea and try developing with ASP.NET MVC. Again thank you for your help.
-
Would probably be easier to use Linux!