PHP OCI: Connection string (convert from JDBC) - php

I have below obfuscated connection string in SQL developer which works:
jdbc:oracle:thin:#//xyz-scan.example.com:1521/mydb.example.com
How can I use this in php oci_connect?
$db = 'xyz-scan.example.com:1521/mydb.example.com';
$con = oci_connect('scott', 'tiger', $db, 'AL32UTF8');
Lead to error:
ORA-12545: Connect failed because target host or object does not exist
I can ping the server successfully.
I also tried
$db = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST = xyz-scan.example.com)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SID=mydb.example.com)';
And instead of SID with service_name. Nothing works.
Above gives this error:
ORA-12154: TNS:could not resolve the connect identifier specified
How do I convert this connection string to work with php oci? (is there a unique way? For a different db I have one with #ldap://... how would I convert that?

The solution to first issue with scan address is simple:
$db = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST = xyz-scan.example.com)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SID=<sid>)';
The second one with ldap is not so simple. As far as I understood oracle has it's own ldap thingy and in it you store the databases TNS entries. This means you query it like you query any ldap system:
<?php
$ds=ldap_connect("oid.mydomain.com", myport); // Connect to oracle ldap
$r=ldap_bind($ds); // Bind to ldap
$sr = ldap_search($ds, "cn=OracleContext,dc=xyz,dc=abc,dc=com", "cn=dbname"); // Run query xyz.abc.com
$info = ldap_get_entries($ds, $sr); // Get entries
ldap_close($ds);
$dbconnectstring = $info[0]["orclnetdescstring"][0]; // Extract db connect string from ldap search result array
$con = oci_connect('scott', 'tiger', $dbconnectstring);
?>
This script will get full TNS connection string which you can then use with oci_connect.

Related

how to use pg_connect to connect to 2 different postgresql databases on the same computer

I am trying to use 2 instances of postgresql database on the same computer. any idea how should i use pg_connect
I was able to connect to one instance of postgresql using pg_connect successfully. However I have created a second instance for a new web application. How to specify parameter to connect to both databases on the same php code using pg_connect.
It is literally the first example given on php.net reference page for pg_connect
$dbconn = pg_connect("dbname=mary");
//connect to a database named "mary"
$dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");
// connect to a database named "mary" on "localhost" at port "5432"
$dbconn3 = pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//connect to a database named "mary" on the host "sheep" with a username and password
Just change the connection string and declare multiple connection objects and you should be fine

Connecting to a mysql database without keeping details in the script

I am attempting to create a separate login file for database connections as I am not too fond of having all the access details on each page that requires database access.
I have created a separate file on my server that contains the variables required for a successful login and then use the;
include_once('path_to_file/filename.php');
to get the variables and then use;
$dbconnection = mysqli_connect("$hostname","$username","$password","$database") or die ("Could not connect to the server");
but the connection fails every time. I tried including the connection script in the file I am attempting to include but then I get this message:
Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)
I'm not really sure how to fix this, but every page in my server more or less access the database and I think it has to be a security risk having login details replicated everywhere!
Anyone have any suggestions or alternatives?
databaseloging format is:
<?php
# parameters for connection to MySQL database
$hostname="hostname";
$database="databasename";
$username="username";
$password="password";
?>
P.S. I have also tried require and got the same result.
Also when using multiple MySQL connections in PHP, you have to supply a fourth argument telling PHP to actually create new connections like this (this is very important, if you are using two connections to the same host):
$db1 = mysql_connect($host1, $user1, $passwd1, true);
$db2 = mysql_connect($host2, $user2, $passwd2, true);
If the fourth argument is not used, and the parameters are the same, then PHP will return the same link and no new connection will be made.
After this you should use "mysql_query" with an extra parameter than defines which connection to use:
$res1 = mysql_query($sql1, $db1) or die(mysql_error($res1));
$res2 = mysql_query($sql2, $db2) or die(mysql_error($res2));
http://www.php.net/manual/en/function.mysql-connect.php

Trying to connect with sql server database

I'm trying to connect sql server with php, i'm trying to get info from the database..
Now, here is what i got:
try {
$user = '';
$pass = '';
$objDb = new PDO('mysql:host=192.168.10.250;dbname=WEB_POROSIA',
'$user', '$pass');
$objDb->exec('SET CHARACTER SET utf8');
$sql = "SELECT *
FROM 'WEB_POROSIA'
";
$statement = $objDb->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
}
I receive this error:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
This is my first attempt to connect php with sql server, and i don't know what this output means, i mean i don't know what might cause it!
I'm using xampp.
Thanks
It appears that you are trying to connect using the wrong driver.
For Sql Server you might want to use PDO ODBC driver not the mysql method you are trying to use.
You need to use a different DBO driver.
Check out other ODBC drivers, i.e PDO.
The code you have written tries to connect to a MySQL database rather than a MSSQL one.
You'll want to do something like this:
$db_connection = new PDO("dblib:dbname=$db_name;host=$host", $username, $password);
You can find more information here: http://grover.open2space.com/content/use-php-and-pdo-connect-ms-sql-server

php adodb MSSQL connection

I have a linux server that I'm trying to use php adodb to connect to a MSSQL server.
include('adodb5/adodb.inc.php');
$conn =& ADONewConnection('odbc_mssql');
$dsn = "Driver={SQL Server};Server=MSSERVER;Database=Northwind;";
$conn->Connect($dsn,'sa','password')or die("Unable to connect to server");
I've install mssql through yum etc and I know the server can connect to it as I've tried the following:
$db = #mssql_connect("MSSERVER","sa","password") or die("Unable to connect to server");
mssql_select_db("Northwind");
// Do a simple query, select the version of
// MSSQL and print it.
$version = mssql_query('SELECT ##VERSION');
$row = mssql_fetch_array($version);
echo $row[0];
// Clean up
mssql_free_result($version);
Any ideas why my adodb wont connect, or any examples on how I can connect would be much appreciated.
I've solved this by looking at this forum: http://ourdatasolution.com/support/discussions.html?topic=4200.0
The correct code is:
<?php
include("adodb5/adodb.inc.php");
//create an instance of the ADO connection object
$conn =&ADONewConnection ('mssql');
//define connection string, specify database driver
$conn->Connect('xxx.xxx.x.xxx:1400', 'user', 'password', 'DbName');
//declare the SQL statement that will query the database
$query = "select * from table";
$rs = $conn->execute($query);
//execute the SQL statement and return records
$arr = $rs->GetArray();
print_r($arr);
?>
Hope that helps somebody else.
With php 5.3 of above the php_mssql modul is no longer supported for windows.
A Solution is to download the MicroSoft PHP Driver from http://www.microsoft.com/en-us/download/details.aspx?id=20098.
This installer will extract the modul dll files to you php extension directory.
Include the correct version in you php ini (e.g. like this for php 5.3 ThreadSafe):
extension=php_sqlsrv_53_ts.dll
After this you can use adboDb again, but you have to use mssqlnative as adodbtype.
And the connection with ip and port didnt work for me, but ipaddress\\SERVERNAME worked (see examplecode)
<?php include("adodb5/adodb.inc.php");
//create an instance of the ADO connection object
$conn =&ADONewConnection ('mssqlnative');
//define connection string, specify database driver
// $conn->Connect('xxx.xxx.x.xxx:1400', 'user', 'password', 'DbName');
$conn->Connect('xxx.xxx.x.xxx\\SERVERNAME', 'user', 'password', 'DbName');
//declare the SQL statement that will query the database
$query = "select * from table";
$rs = $conn->execute($query);
//execute the SQL statement and return records
$arr = $rs->GetArray();
print_r($arr); ?>
For PHP 7.4 you can download the drivers here:
https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15
Copy the files to the ext directory of your php installation.
In the php.ini file then add the extensions as follows:
extension=php_pdo_sqlsrv_74_ts_x64
extension=php_sqlsrv_74_ts_x64
The extension also requires the ODBC driver for SQL Server to be installed:
https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15

How to retrieve data for a webpage from an MS Access database with password

I have an MS Access database file that I want to copy into a MySQL to serve up on a webpage, the problem is the database is passworded. What I want to do is upload the file to the server then either strip the password or open it using the password so I can then copy it across to MySQL.
The password is known and cannot be removed at source.
I would like to do this with PHP if possible.
This is a recurring event, at max twice a day.
Having contacted my hosting the only way to use odbc is to upgrade to dedicated hosting at 10x the price of my current hosting. Looks like this one is a no go unless I can get at the data another way.
To open it, the password should be passed along in the connection string... For PHP using odbc_connect, the syntax is available here. Since you say the password is known, this should work.
To remove it completely, you'd want to just open it in Access and save a copy without the password. I'm not sure that this can be automated easily. If you need to access the data and transfer it repeatedly, I'd say stick with the password int he connection string.
Example from the article linked to:
<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>
Here is the DSN - Less connection code sample :
<?php
$db_connection = new COM("ADODB.Connection");
$db_connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../databases/database.mdb") ." ;DefaultDir=". realpath("../databases");
$db_connection->open($db_connstr);
$rs = $db_connection->execute("SELECT * FROM Table");
$rs_fld0 = $rs->Fields(0);
$rs_fld1 = $rs->Fields(1);
while (!$rs->EOF) {
print "$rs_fld0->value $rs_fld1->value\n";
$rs->MoveNext(); /* updates fields! */
}
$rs->Close();
$db_connection->Close();
?>

Categories