configuring php with oracle usin tns on mac os x - php

I am new to php but I've got it running (php 5.3.3) on my mac (OS 10.6). However when I try to run this script:
<?php // Create connection to Oracle
$conn = oci_connect("user", "pass", "tnsnames.ora");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
?>
I get the error: "Fatal error: Call to undefined function oci_connect()..."
Now, I've checked oracle's website and downloaded their instant client, but I'm stuck as to what to do next. Their instructions are:
On non-Windows platforms rebuild PHP
using the following configuration
option:
--with-oci8=shared,instantclient,/path/to/instant/client/libs
Edit your php.ini file and add:
extension = oci8.so Ensure that your
extension_dir parameter (in php.ini)
points to the location where oci8.so
was installed. Set environment
variables required by Oracle, such as
PATH (Windows) or LD_LIBRARY_PATH (on
Linux) Restart you webserver.
But could someone explain that to me in simpler language? I am really confused. I can't find an oci8.so file in the instant client folder, and I don't know where to put the extention = oci8.so in the file (all I have are php.ini-production and php.ini-development. And I don't know how "rebuild" PHP with configuration options.
Oh and I don't know how to pull in the tnsnames.org file either. Is this the right way? Since I don't even have a connection yet, I don't know if this is failing or not.

You don't want the filename "tnsnames.ora" in your oci_connect call. You want the name of a database connection alias in the tnsnames.ora file. The tnsnames.ora file contains a list of known database connection entires, identified by aliases. For a database alias named "db_alias", you would use this call:
$conn = oci_connect("user", "pass", "db_alias");
A typical tnsnames.ora entry would look something like this:
DB_NAME =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = foo.bar.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE = some_db_service_name))
)
Alternatively, you can use an Easy Connect string:
$conn = oci_connect("user", "pass", "//host:port/db_service_name");
Where host, port and db_service_name are to be replaced by values from your environment. So, for host "foo" with database "bar" on standard port 1521, your connection would look like this:
$conn = oci_connect("user", "pass", "//foo:1521/bar");
Have a look at the manual page for oci_connect for more information.

I do it with a full connect string:
$rnum=rand(0,99999999);
$connect_str = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = $dbserver) (PORT = $dbport) (HASH = '.$rnum.') ) (CONNECT_DATA =(SID = $dbname)) )";
$DB = oci_connect($dbuser, $dbpass, $connect_str);
Works like a charm for me.

Related

How to solve ORA-12154: TNS error when using PHP OCI8 oci_connect in Azure

I'm trying to connect to the oracle database of a new partner, 11g Release 11.2.0.4.0, and I'm receiving the following error:
Warning: oci_connect(): ORA-12545: Connect failed because target host or object does not exist
The partner affirms that the connection string provided is correct.
This is the current structure of mine oci_connect() and the said connection string being used:
$connection_string = '(DESCRIPTION = (ADDRESS_LIST = (LOAD_BALANCE = ON)(FAILOVER = ON)(ADDRESS = (PROTOCOL = TCP)(HOST = HOST)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = SERVICE_NAME)))'
$conn = oci_connect($username, $password, $connection_string, 'UTF8', OCI_DEFAULT);
But, when using the following structure:
$connection_string = 'user/password#host:port/service_name'
$conn = oci_connect($username, $password, $connection_string, 'UTF8', OCI_DEFAULT);
It returns:
Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified
To give a clear picture of what I did, below are the steps I took to create my App-Service and install the oci8 php driver:
I created an Azure App-Service, that uses PHP, Version 7.3.26;
Created a new oracle directory inside /home/site;
Added the instant client to the new oracle directory, in this case it was the Instant Client Linux x86-64 18.5.0.0.0v (bouth the basic and the sdk packages);
Proceded to use the command pecl install oci8-2.2.0;
I pointed out the location of the instant client during the installation process, the oci8 driver was installed successfully;
Copied the new oci8.so file to an ext directory inside /home/site;
Created a php.ini file, with extension=/home/site/ext/oci8.so set in;
Defined the PHP_INI_SCAN_DIR config as being /usr/local/etc/php/conf.d:/home/site/ini;
Defined the LD_LIBRARY_PATH as being /home/site/oracle/instantclient_18_5.
After that I could confirm the existence of the oci8 extension on my phpinfo() page.
Those steps where made following this documentation: Azure App Service Linux - Adding PHP Extensions
I also have latter defined the ORACLE_HOME setting as being /home/site/oracle/instantclient_18_5, but it had no diferences.
I'm out of ideas, can someone please indicate me what could be causing this problem and how can I solve it.
EDIT:
After further conversations with the partner it becamed clear that there isn't no way for me to connect to their database considering the current configuration of the same. As noted int the comments by Christopher Jones, its a problem of networking, ORA-12545, ORA-12541, ORA-12514, & ORA-01017 – How to fix for SQL Developer, and, in this case, lack of proper feedback.

Call to undefined function sqlsrv_connect() - Troubleshooting

System Information
CMS: Wordpress
Web Server: XAMPP
PHP Version: 5.5.30
MS Management Studio 17
Goal
Establish MSSQL Database connection using PHP
What has been done
Downloaded SQLSRV Drivers
Copied files php_pdo_sqlsrv_55_nts.dll and php_pdo_sqlsrv_55_ts.dll to the directory C:\xampp\php\ext
Added the following lines to the dynamic extensions part in the php.ini file: extension=php_pdo_sqlsrv_55_ts.dll and extension=php_pdo_sqlsrv_55_nts.dll
Restarted Web Server
Confirmed sqlsrv is listed in phpinfo()
Code
$serverName = "technology-pc\sqlexpress";
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"example_db");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
} else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
Error
Call to undefined function sqlsrv_connect()
You have added the PDO variant of SQLSRV drivers to the extension list, but have not added the base drivers php_sqlsrv_55_ts.dll.
Add to the php.ini:
extension=php_sqlsrv_55_ts.dll
or
extension=php_sqlsrv_55_nts.dll
Also, you really should be using either the Thread-Safe (_ts.dll) or Non-Thread-Safe (_nts.dll) versions of the driver, not both. I believe that, as you are using an Apache Server, you should be using the Thread-Safe versions. So you php.ini should have:
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
You probably edited the wrong php.ini file. Check the right php.ini file with php info.
You can use this script:
<?php echo phpinfo(); ?>
Or if you have CLI access type php -i to get the info listed.
Check for the right path of your php.ini file.
Try below code to connect mssql database
$server = 'dburl.com\MSSQLSERVER, 1433';
$username = 'uname';
$password = 'password';
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password,"ReturnDatesAsStrings"=>true);
$conn = sqlsrv_connect( $server, $connectionInfo);

oci_connect ERR_CONNECTION_RESET when using tnsnames

I am trying to connect to an oracle database using php. When I try to connect by connection string/descriptor, it connects just fine, but when I try to connect using a tns name, the whole thing stops and chrome shows a blank error page ERR_CONNECTION_RESER. I see the failed response and it is 0 bytes, no header, no body... nothing.
here is the code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$connStr = "Some connection <string> copied from tnsnames.ora";
//$connStr = "Some connection <name> copied from tnsnames.ora";
try
{
$conn = oci_connect('dev', '12345678', $connStr, 'utf8');
if (!$conn)
{
$err = oci_error();
var_dump($err);
}
oci_close($conn);
echo "Success";
}
catch(Exception $e)
{
$err = oci_error();
var_dump($err);
var_dump($e);
}
I checked and my php can see
ORACLE_HOME
TNS_ADMIN
and they are pointing to the right direction.
I am using
Windows 7 amd64
PHP 5.6.13 TS VC11
Apache 2.4.16 VC14
Instant Client 12.1.0.2
After hours spent and sleeps not slept on this issue, I found the solution.
I have an sqlnet.ora file that specifies timeouts and other network properties. Our databases recently joined the firm's domain and the problem was with this line:
Previously
NAMES.DIRECTORY_PATH= (HOSTNAME, TNSNAMES, ONAMES, EZCONNECT, LDAP)
#########
Solution
NAMES.DIRECTORY_PATH= (HOSTNAME, TNSNAMES, ONAMES, EZCONNECT, LDAP)
NAMES.DEFAULT_DOMAIN = FIRM.DOMAIN

Apache will not connect to SQL Server with PHP

I'm trying to open a connection to a SQL Server 2008 on a windows computer from an apache linux server via php. I've opened up the appropriate port on the firewall, but I am getting the vaguest error
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxx.xxx.xx.xxx,xxxx
with:
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
$myUser = "un";
$myPass = "pw";
$myDB = "db";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());
Is there some way to get a more specific error message? Or some way that I can test to see if the two computers are communicating at all so I can try to begin to localize the problem?
Here's the code I use to connect PHP to MSSQL from Ubuntu machines to Windows SQL Server, I don't know if it will help you or not but this code is up and running successfully right now so I know it works in our environment...
As you can see, I use PDO rather than the mssql_* functions. On Ubuntu I needed to install the php5-sybase package to get the dblib driver.
PHP:
<?php
try{
$con = new PDO("dblib:dbname=$dbname;host=$servername", $username, $password);
}catch(PDOException $e){
echo 'Failed to connect to database: ' . $e->getMessage() . "\n";
exit;
}
?>
/etc/odbc.ini
# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssqldb]
Description = MSSQL Server
Driver = freetds
Database = MyDB
ServerName = mssqldb
TDS_Version = 8.0
/etc/odbcinst.ini
# Define where to find the driver for the Free TDS connections.
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
/etc/freetds/freetds.conf
[global]
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# Define a connection to the MSSQL server.
[mssqldb]
host = mssqldb
port = 1433
tds version = 8.0
I ran into the same problem today. This works for me:
Instead of this
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
try this:
$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file
In your case, I'd rename the entry and use the fully qualified hostname to the entry in the config file just for clarity
# Define a connection to the MSSQL server.
[mssqldbAlias]
host = mssqldb.mydomain.com
port = 1433
tds version = 8.0
The first argument in the call to mssql_connect() must correspond to an [entry] in the freetds.conf file.
So your call becomes
$myServer = "mssqldbAlias";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());

PHP: Unknown MySQL server host with mysqli

I have the following code:
$username = 'username';
$password = 'password';
$host = 'localhost';
$db = 'dbname';
$dbh = new mysqli('p:'.$host, $username, $password, $db)
or die('no connection to server');
But I get the following error
PHP Warning: mysqli::mysqli() [<a href='function.mysqli-mysqli'>function.mysqli-mysqli</a>]: (HY000/2005): Unknown MySQL server host 'p:localhost' (1) in /var/www/vhosts/politiker.lu/httpdocs/includes/sql.php on line 8
This intrigues me because the very same code worked on my development environment. Now that I want to set it up on my production server, it does not work. Any ideas?
Note: I am not very good with servers, so excuse me if the error should be something trivial.
EDIT 1
Here are the versions:
Development PHP (5.3.3-1ubuntu9.1) + MySQL(5.1.49-1ubuntu8.1)
Production PHP (5.2.4-2ubuntu5.12) + MySQL(5.0.51a)
take out the p: like this:
$dbh = new mysqli($host, $username, $password, $db)
or die('no connection to server');
Just adding this since I didn't think it was clear, but the problem is definitely your production version of PHP. Persistent connections for the mysqli extensions weren't added until 5.3 since they caused headaches before.
Some hosts might have different versions of PHP installed... I realized today that I can make HostGator use PHP 5.3! I just had to add to my .htaccess file:
Action application/x-hg-php53 /cgi-sys/php53
AddType application/x-hg-php53 .php
i think you missed the mysqli package in this server. Try :
sudo apt-get install php5-mysqli
if apt-get says it is already installed try to do a ping to localhost, if don't resolve edit /etc/hosts and check the line where localhost points to 127.0.0.1
should be there, if not, your server its horribly configured!
do a
ifconfig
and check if you get some output like this...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
You need to specify the actual location of the MySQL server. For development, it is localhost (most likely), just how you have it configured. But for production servers, it is going to be something else. I doubt it should be localhost.

Categories