PHP to Oracle using oci8/MDB2 - _doConnect: unable to establish a connection - php

I'm running a Linux Fedora 19 setup with Oracle 11g, PHP 5.5.4, Apache 2.4.6, and PEAR 1.9.4. In addition to this, I have the OCI8 plug-in for PHP downloaded and enabled, and have downloaded MDB2 and the MDB2_Driver_oci8.
The goal is to connect my webpage to the Oracle 11g database. This already works in an existing setup (which I didn't do), and I'm trying to replicate it in a new setup.
For some reason, in the new server, it doesn't work. It generates an error:
_doConnect: [Error message: unable to establish a connection]
** oci8 (oci8)://gbsihr:xxx#localhost:1521/
An error occurred while trying to connect to the database server.
Error message: MDB2 Error: connect failed
I truly have no idea what this means. This is my PHP connect function:
<?php
include_once('includes/configure.inc');
require_once 'MDB2.php';
function DB_connect($intSilent = -1){
$db_dsn = $_SESSION['DB_Type'] ."://";
$db_dsn .= $_SESSION['DB_Username'] .":";
$db_dsn .= $_SESSION['DB_UserPassword'] ."#";
$db_dsn .= $_SESSION['DB_Host'] .":";
$db_dsn .= $_SESSION['DBPort'] ."/?service=";
$db_dsn .= $_SESSION['DB_DbName'];
if (PEAR::isError($dbconnect)) {
print '<pre>';var_dump($intSilent);print '</pre>';
print '<pre>';var_dump($db_dsn);print '</pre>';
print '<pre>';print $dbconnect->getDebugInfo();print '</pre>';
echo "An error occurred while trying to connect to the database server.<br>\n";
echo "Error message: " . $dbconnect->getMessage() . "<br>\n";
echo "A more detailed error description: " . $dbconnect->getDebugInfo() . "<br>\n";
exit();
// Check whether the object is a connection or
if($_SESSION["gENDebugInfo"] == "On"){
$_SESSION["gErrorNote"] = $dbconnect->getDebugInfo();
}
if ($intSilent < 0) {
header("Location:" . $_SESSION["gENWebRoot"] . "showerror.php?ec=1003"); // Failed to connect
exit();
}
}else{
$dbconnect->setOption('portability', MDB2_PORTABILITY_ALL);
}
return $dbconnect;
}
?>
And this is the contents of configure.inc:
$_SESSION["gENWebRoot"] = '/hrweb/';
$_SESSION['DB_Type'] = 'oci8';
$_SESSION['DB_Host'] = 'localhost';
$_SESSION['DB_Username'] = 'gbsihr';
$_SESSION['DB_UserPassword'] = 'ids21';
$_SESSION["DBPort"] = '1521';
$_SESSION['DB_DbName'] = 'xe';
As this works on another setup, I'm not sure where the error lies. OCI8 seems to be in order, though I did go through hoops to get it working.
I did do some research, with only two vague results - one pointing to the oci8.php itself, and the other, an Oracle TNS error (unlikely, given the lack of errors).
Here are my attempts to solve the problem:
I tried copying the Basic Query Example from this website. I modified the "hr" and "welcome", and ran it on my new setup (it works fine on the older one). And this is the result:
oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with
your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are
set and point to the right directories. Error while trying to
retrieve text for error ORA01804
I've also tried opening the httpd file in /etc/sysconfig/ and adding the following line to it:
LD_LIBRARY_PATH="/u01/apps/oracle/product/11.2.0/xe/lib
export LD_LIBRARY_PATH
And yet when I try to echo $LD_LIBRARY_PATH in the CLI, it won't return anything. It does appear in phpinfo(), though:
This is in contrast to $ORACLE_HOME, which I have to load manually every start-up by typing in . /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh in the CLI, and does not appear under environments, but at least, can be echo'd.
There are a lot of variables in the old setup under environment; LD_LIBRARY_PATH, ORACLE_HOME, and ORACLE_SID being the most noteworthy. I only see LD_LIBRARY_PATH in the envvars file, though. Should I add them as well to the httpd file?
I'm not much of a Linux-person, sorry. Any help on this would be much appreciated.

I suggest you try to connect to your oracle database by using the plain oci8 functions and the same login parameters as you use for MDB2.
My guess is that this will fail as well, but might give you more error details.

Can you connect to oracle outside of php?
I presume you have a valid tns.ora file set up.
I needed these two lines set in my VirtualHost definition, note TNS_ADMIN is defined also, perhaps you do too:
SetEnv ORACLE_HOME /opt/oracle/instantclient
SetEnv TNS_ADMIN /usr/lib/oracle/11.2/client/network/admin/network/admin**

Just add this line
$dbconnect = DB::connect($db_dsn);
to function DB_connect
like this
...
$dbconnect = DB::connect($db_dsn);
if (PEAR::isError($dbconnect)) { ...

Related

Installing drivers for SQL Server in PHP

I'm new to PHP. PHP is installed and working, and now I'm trying to set up connection with a SQL Server DB. Following instructions from a Youtube video. Went through and finished installing the drivers, but it isn't working. My test page stops running code after the sqlsrv_connect() function is called. I'm not even getting an error message. Below is my test page code:
<?php
echo '<br><span style="font-weight:bold;">PHP functioning. This line is being echoed out.</span><br />';
/*No problems yet...*/
$serverName = "Myserver";
$connectionInfo = array('Database'=>'nde', 'UID'=>'sa', 'PWD'=>'!hegEcu&3ATr');
$conn = sqlsrv_connect($serverName, $connectionInfo);
/*Nothing is appearing on the screen after this */
echo 'Still working...<br />';
if ($conn) {
echo 'Connection established.<br />';
} else {
echo 'Nope.<br />';
die(print_r(sqlserv_errors(), TRUE));
}
?>
I'm running PHP 7.0.21, and SQL Server 2014. I went and downloaded the two DLL files I think I need - php_pdo_sqlsrv_7_ts_x64.dll, and php_sqlsrv_7_ts_x64.dll. I copied them to Program Files/PHP/v7.0/ext. I went into PHP.ini as admin, and typed:
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_sqlsrv_7_ts_x64.dll
in the Windows Extensions sections. I saved PHP.ini, restarted both the IIS Admin Service and World Wide Web Publishing Service, and went into IIS Manager and restarted the server. PHP is still working, but I'm still not getting any echo after the sqlsrv_connect() call.
Okay, I got it working. As ctwheels suggested, i just installed the nts driver versions instead of the ts versions, which are php_pdo_sqlsrv_7_nts_x64.dll and
extension=php_sqlsrv_7_nts_x64.dll. It's working with sqlsrv_connect(); I haven't tried using PDO. I'm new to PHP and unfamiliar with PDO, but I'll probably come across it again, especially as I start looking into PHP frameworks (Cake and CodeIgniter).
One important thing to share for anyone who might also be new to PHP - the PHP documentation says that the $serverName argument of sqlsrv_connect() should be in the format of servername/instancename. So I used this query to get both: select ##servername + '\' + ##servicename, and got "Myserver\MSSQLServer". However, using this as the $serverName argument caused the connection to fail. Only after I took out "\MSSQLServer" did it connect successfully. So as in the code above, the correct value to use was $serverName = "Myserver".

Cannot connect to MySQL Database with MDB2 (Error not Found)

In first point. I am absolutely a noob in PHP and PEAR. For exercising I have worked with PHP, Pear and Mondial DB of Oracle offline, but now I wanted to connect to my Database on 1and1.com.
Following I have tried:
$dsn = 'mysql://dbo5235xxxxx#10.24.xxx/db5235xxxxx'; //Have it tried with password too and many other variations
$sql = "SELECT * FROM Vereine";
$db = MDB2_Util::connect($dsn);
If I upload this file on my webserver and when I try to call this page, I get an error.
Fehler beim Verbindungsaufbau mit [mysql://dbo5235xxxxx#10.24.xxx/db5235xxxxx] : MDB2 Error: not found
The error Message:
"Fehler beim Verbindungsaubau" is an own deinied Message in MDB2_Util.
If I try to connect with MDB2::connect, then the Message calls only:
MDB2 Error: not found
What can be the Error? Why it doesnt show the real Error or a helpful hint. Can I debug? If yes, how?
Best Regards Benny
This can occur even if you have installed an MDB2 driver, but PHP can't find or can't read it.
As a practical example, I experienced this problem on a system on which the UMASK value had been changed from 022 to 027. Even though the MDB2 driver had been installed (with root privileges), the user under whom php-fpm was running lacked access to the library's files.

Cannot access SQLite3 db using php

I've spent many-a hour trying to get a local webserver working (I'm new)
I created a sqlite3 database ('database.sql') in the www folder, and tried calling it with numerous different php commands (php 5.5, I checked) such as '$test = new SQLite('database.sql')' or the same with SQLite3, both with no luck. Also tried $test->open('database.sql').
Always with the fatal error "Class 'SQLite' not found". I've spent too many hours on what I'm sure is a very simple problem, I'm sorry to have to ask this!
To open a DB using PHP5 and SQLite we need to use a PDO and not the sqlite_open() function.
This is how to open or create a database: (not sure if it's bug free)
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:VPN0.sqlite");
echo "Handle has been created ...... <br>";
}
catch(PDOException $e) {
echo $e->getMessage();
echo "<br>Database is loaded UNSUCCESSFULLY .. ";
die("<br>Query is closed $error");
}
echo "Database loaded SUCCESSFULLY ....";
Hope this helps!
There is a php package called sqlite3 which you can use (as well as PDO which is given above). Here is a fraction of code which uses it.
$db = new SQLite3(DATABASE);
if (isset($dbversion)) { //only newer versions of chat will have this
$version = $db->querySingle("SELECT value FROM parameters WHERE name = 'db_version'");
Where the DATABASE variable has been defined with
define('DATA_DIR',$datadir); //Should be outside of web space
define('DATABASE',DATA_DIR.'chat.db');
Are you sure that the package is installed on your server?
If you're still having problems you can do a couple of things. First, use phpinfo(); in a page to determine if the SQLite3 extension is installed.
Second, if you want to use PDO make sure that the following line in your php.ini is un-commented:
extension=php_pdo_sqlite.dll
If you have to un-comment this you will need to restart your server for the changes to take effect.

mssql_connect() with PHP5 and MSSQL2012 Express

I'm having heavy issues trying to connect to my MSSQL 2012 Express database, with PHP5 running on an Apache.
I have as a test setup just installed a XAMPP with PHP 5.4.4 and Apache running on a Windows 7 machine.
My PHP code (phpmssql_genxml.php):
$connection = mssql_connect('192.168.40.150', $username, $password);
if (!$connection) { die('Not connected : ' . mssql_get_last_message());}
$db_selected = mssql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mssql_get_last_message());
}
$query = "SELECT * FROM Cust WHERE 1";
$result = mssql_query($query);
if (!$result) {
die('Invalid query: ' . mssql_get_last_message());
}
Output when trying to enter the site:
Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\phpmssql_genxml.php on line 13
Even if I try to hardcode the username and password into the string, I still get the same result.
Have search a lot on google, but havn't found that post that fixed my issue yet :/
Have enable TCP/IP for the DB instance pipe, even try'd to assign a specific TCP port for it. Have created a rule in the Win7 firewall allowing all traffic to the standard port 1433. Still no luck.
any1 have an idea?? What does the 'Fatal error' part means? Is it the Apache error, PHP or a Database error when trying to connect to it??
You are missing MSSQL driver from your PHP setup. Download it from here, assuming you have the required system configuration mentioned on the page.
Setting up, from their instructions:
Download SQLSRV30.EXE to a temporary directory
Run SQLSRV30.EXE
When prompted, enter the path to the PHP extensions directory
After extracting the files, read the Installation section of the SQLSRV30_Readme.htm file for next steps
I would also recommend using standard Apache + PHP installation, if you plan to work with MSSQL, instead of any *AMP package.
You don't need to use SQLSRV30 as this was not the solution that I needed though some may find it useful. I have a local dev environment running XAMPP + php5.5.9 + MSSQL2012 and I needed to use mssql functions because thats what we use at work on our servers. So I use freetds and "luckily" found this:
https://moodle.org/mod/forum/discuss.php?d=232844
where one of the users already compiled the php_dblib.dll TS and NTS for my php version suffice to say I have everything working and using all the MSSQL functions that our dev team is used to. The extension for mssql support can always be compiled.

Using PHP/oci8 to Connect to Oracle Remote DB - Unsure how to use listener

Overall goal of my project is to connect to a Oracle database that is on another server, to query it using PHP, so that I can create charts of the data using JavaScript.
My server is running CentOS
So far I have followed the directions out on the web and have installed oci8 on my server as well as Oracle Insant Client.
I then created a shell script to tunnel to the remote server.
Next I created a test php file to try and connect to the database
<?php
$conn = oci_connect('name', 'pw', 'servername/databasename');
if(!$conn){
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
oci_close($conn);
?>
When I load this in the browser I am getting the following error
Warning: oci_connect(): ORA-12541: TNS:no listener in /var/www/html/djc/ociConnect.php on line 3 Fatal error: ORA-12541: TNS:no listener in /var/www/html/djc/ociConnect.php on line 6
I have done my research about the error, and I know the major problem is
lsnrctl start
does not work, I have no lsnrctl function. I also do not have a TNSNAMES.ORA, or LISTENER.ORA file.
I do not know how to get lsnrctl to work on my server, or if I am even attacking this problem from the correct angle.
Also trying to start sqlplus(which I installed from rpm), returns command not found.
Any suggestions?
I am not even sure if I am going about achieving my goal in the correct manor, so any help is greatly appreciated.
I used to connect with:
//Putenv("NLS_LANG=SPANISH_SPAIN.WE8ISO8859P15");
$db="(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=TCP)
(HOST=$GLOBALS[dbhost])(PORT=$GLOBALS[dbport])
)
)
(CONNECT_DATA=(SID=$GLOBALS[dbname]))
)";
$conn = OCILogon($GLOBALS['dbuser'],$GLOBALS['dbpasswd'],$db);
Replace $GLOBALS['dbuser'], $GLOBALS['dbpasswd'], $GLOBALS[dbhost], $GLOBALS[dbname], $GLOBALS[dbport] accordingly.

Categories