Connect to MS Access remote .mdb file from php on linux - php

I have been digging internet for couple days, reading very old information, that leads to very old and nonexisting sites, still, I understood, what is needed to achieve my goal.
We have a file.mdb on server running WindowsXP, so I need to add it to ODBC data sources. I do that with simple steps, ending up with "System DSN", that allows access to that .mdb file
I need to install on this same server some sort of ODBC bridge, that would allow me to create remote connection to this server, making that bridge connect to servers ODBC DSN, and query out my stuff (could not find any free ODBC bridge)
On UNIX (FreeBSD) machine, I need to install unixODBC and php5-odbc packages, enabling connections to ODBC (already installed)
To connect to remote ODBC and use MS Access db driver, I need to have such a driver for unixODBC, in .so file, that is sitting inside UNIX machine (could not find any free MS Access drivers)
Connect to that server using PHP odbc_connect(DSN,user,password), and in DSN I need to give some connection information and driver, which I need to use (MS Access driver).
Correct me, if I'm mistaken and please give me more advice, how to achieve such a connection.

Finally, I found solution.
Set up on Win server FreeSSHd, configure connection account and set directory to one, you need
Set up on unix server sshfs
Mount Win server directory with .mdb files
sshfs {user}#:/ {unix mount point} -o workaround=rename,allow_other
Set up on unix server mdbtools
So, I used default PHP code from docs and write this PHP script:
$rows = $cols = array();
if (($handle = popen('/usr/bin/mdb-export {unix mount point}/{file}.mdb {table} 2>&1', 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) { for ($c=0; $c < $num; $c++) { $cols[] = $data[$c]; } }
else { for ($c=0; $c < $num; $c++) { $rows[$row][$cols[$c]] = $data[$c]; } }
$row++;
}
pclose($handle);
}
print_r($rows);
Path to /usr/bin/mdb-export should be path to your mdb-export file (use find / -name "mdb-export", if you can't find yours).
Mount point {unix mount point} should be an empty file folder (I used /usr/home/remotemdb)
Table {table} should be the table name inside mdb file. Query all possible tables inside mdb file with command mdb-tables {unix mount point}/<file>.mdb
There is no need for drivers, configuration or other stuff, just plain mdbtools and access to file, in this case, achieved with remote connection through ssh. In you want, you can install fuse package, to autmatically mount remote directory, but that is another question.
Hope someone this helps.

You don't connect to a "server dsn". DSN's are a local thing only. They're not exposed for remote connections at all. If you want a machine to connect to a database, you need to have a DSN configured on that machine - you won't be able to use a DSN specified elsewhere.
For PHP ODBC, that'd be
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=/network/path/to/your/access/database.mdb", $user, $password);

You are correct insomuch that you require an ODBC to ODBC Bridge.
At OpenLInk we refer to a Multi-tier ODBC to ODBC Bridge...
This is Multi-tier in the sense that it has a client/server architecture as follows --
Linux Client --
ODBC Application
OpenLink Generic ODBC Driver
Windows Server --
32bit OpenLink request Broker
32bit OpenLink ODBC Agent
32bit Microsoft Access ODBC Driver (with pre configured DSN)
Microsoft Access Database file.

Its commercial, so possibly not of interest, but Easysoft have an ODBC driver for Access that's available on Most *nix's. No bridge required. There isn't a build on FreeBSD at the moment, but I could get one built for you on Monday if it's of any interest.
There is the open source MDB tools that may have enough for what you want, but it is lacking in quite a lot of functionality.
Easysoft Access ODBC Driver
MDB tools

Use PDO with MDBTools:
install:
apt-get install libodbc1
apt-get install libmdbodbc1
apt-get install php5-odbc
(restart apache)
Sample:
$query = 'SELECT * FROM Table';
$mdb_file = 'file.mdb';
$driver = 'MDBTools';
$dataSourceName = "odbc:Driver=$driver;DBQ=$mdb_file;Uid=user;Pwd=pass;";
$connection = new \PDO($dataSourceName);
$result = $connection->query($query)->fetchAll(\PDO::FETCH_ASSOC);
print_r($result);

Related

Using Cassandra PDO Driver on Windows

Is there any way to have Cassandra PDO at Windows with Wamp?
This is for development purposes I don't want to install Linux and change all the environment.
https://code.google.com/a/apache-extras.org/p/cassandra-pdo/
I'm using Windows 7 (64 Bit), Wamp 2.5, PHP 5.5.
OK, here's what I found out:
1) It's totally possible
2) The docs that appear in the first google search results are a bit obsolete
Start by downloading the latest Datastax Community Cassandra here:
http://planetcassandra.org/cassandra/
Install & setup properly. In fact, most of the configuration is done by the installer, you just have to edit the apache-cassandra/conf/cassandra.yaml file to find all paths to /var/lib... and change those into something like d:/cassandra/... That includes "commitlog", "data", "saved_caches". Restart the Cassandra service, examine the logs. Mine shown no problem. The OpsCenter at ...:8888/opscenter/index.html was working fine, showing one node online.
Now, the PHP part.
There's a sneaky thing called Thrift. From what I've learned today (I first heard about Cassandra and Thrift yesterday), it's a way describe a binary protocol of connecting to some online service, in this case, to Cassandra. It will basically generate PHP files that will provide all the connectivity you need from PHP itself (no extensions needed).
You will need:
1) The Thrift PHP libs
2) The .exe Thrift compiler
Both can be downloaded here:
https://thrift.apache.org/download
Then use the following command to compile PHP files that will act as a "driver" to connect your PHP applications to Cassandra:
thrift --gen php D:\DataStaxCommunity\apache-cassandra\interface\cassandra.thrift
Put the result in some PHP include_path folder.
Also, find the PHP Thrift libs (in the libs archive from the same download page) and put those in a folder accessible to your script (e.g. include_path or the project folder).
Refer this page:
thrift.apache.org/lib/php
I guess that should help!
I have same problem as you, but when i tried this method, it works correctly for me.
Reference link
Here is a code example, very easy to understand :
<?php
require_once 'Cassandra/Cassandra.php';
$o_cassandra = new Cassandra();
$s_server_host = '127.0.0.1'; // Localhost
$i_server_port = 9042;
$s_server_username = ''; // We don't use username
$s_server_password = ''; // We don't use password
$s_server_keyspace = 'cassandra_tests';
$o_cassandra->connect($s_server_host, $s_server_username, $s_server_password, $s_server_keyspace, $i_server_port);
$s_cql = "CREATE TABLE carles_test_table (s_thekey text, s_column1 text, s_column2 text,PRIMARY KEY (s_thekey));";
$st_results = $o_cassandra->query($s_cql);

PHP Connect to MSSQL From Both Linux and Windows

I am using a linux build server (http://circleci.com) for auto builds and testing; however i need to connect to a MSSQL server. I can develop on windows fine and connect using the MS php SQLSVR drivers, but i cannot connect on the Linux build server as the drivers are windows only.
My Question is this: is there a single way to connect to MSSQL server via php for both windows and linux? tried odbc but then you need bdlib and FreeTDS on linux - this would constitute a code change thus meaning the tests are not 100%, for example on windows environment it would use odbc:{SQL Server} and on linux it would be odbc:FreeTDS
It just doesn't seem right to have a check to see what system is being used and then choose the corresponding db connection string.
I am using PHP 5.5.1
No, there is not a single way however it would be trivial to do something like this:
if ('Linux' === PHP_OS){
$pdo = new PDO("dblib:dbname=$database_name;host=$database_server", $username, $password);
}else{
// $pdo = whatever you're using on your Windows box now.
}
You will need to set up your odbc.ini, odbcinst.ini and freetds.conf files on the Linux server as well.

Using MSSQL with PHP on CentOS

I have installed and configured FreeTDS on my CentOS environment to enable my PHP code to get connect to some external MSSQL server. I have my entire application running on written using PDO which is running perfectly on windows. Just wanted to know if my same PDO code can be used even in CentOS or else I have to convert entire code from PDO to mssql_query form.
Like in PDO I have :
$statement_keyword = $obj->conn->prepare($keywordquery);
$statement_keyword->execute();
$rows_keyword = $statement_keyword->fetchAll(PDO::FETCH_ASSOC);
In MSSQL using FreeTDS we have to write
$result = mssql_query($keywordquery);
while ($Row = mssql_fetch_assoc($result)) {
$iw[$i++]=(string)$Row['FullName'];
.......
}
Means I have to run thru entire loop to get the record array, similarly there may be more changes that I need to make like in stored procedures execution ...
Can anyone guide me in using PDO for MSSQL on CentOS, or an easy way other round.
Thanks
After looking for net and doing some research and experimental work I realised it is very easy to use the PDO code for windows to PDO equivalent without must changes, the only change I need to make was in the manner the connection is establised
Using PDO with MSSQL In windows
$this->conn = new PDO("sqlsrv:server=".$this->dbServer.";Database={$this->dbName}",$this->userName,$this->userPassword);
Using PDO with MSSQL on CentOS
$this->conn = new PDO("dblib:host=$this->dbServer;dbname={$this->dbName}",$this->userName,$this->userPassword);
Make sure you have configured FreeTDS, ODBC , etc on your CentOS Stack

Enable Excel PHP PDO driver in Ubuntu Linux 12

This line of code new PDO (" odbc: Driver = {Microsoft Excel Driver (*. Xls, *. Xlsx, *. Xlsm, *. Xlsb)}; DBQ = works fine if my web server is on Windows, but Ubuntu is not true, I can not find how to install the PDO driver for Excel in PHP 5.
Thanks for taking the time on this question.
PHP can manipulate COM objects.
Only in Windows plataform you can do things like:
<?php
$word = new COM("C:\docs\word.doc");
?>
This will create a new instance if there is no running instance available or it will return a handle to the running instance, if available. That's because Windows have ODBC driver working with COM resources.
In your case try use http://www.unixodbc.org/ in Linux, Excel driver are support by this project by http://www.easysoft.com.
The bad news are RMS driver from EasySoft it's not free :(

PHP ODBC Extension - Data Source Name Too Long

I've been trying to install the ODBC extension for PHP onto one of our servers (Redhat), and i think it got it installed correctly but now when i try to test the connection i get the error message about the data source name being too long... It sounds like a simple thing to fix, but i can't work out..how..or where.
Basically these are the settings i've got at the moment:
# odbcinst -j
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN SIze.......: 4
SQLSETPOSIROW......: 2
I've got my MySQL driver defined in the odbcinst.ini as such:
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
FileUsage = 1
I've double checked and the Driver & Setup paths are correct and pointing to the correct files.
So now I'm trying to add a System Data Source by editing the odbc.ini file. I've tried it in various different formats, following examples from different sites, e.g.
http://developer.mindtouch.com/en/kb/Using_the_ODBC_extension_on_Linux#Install_unixODBC
http://dev.mysql.com/doc/refman/5.0/en/connector-odbc-configuration-dsn-unix.html
As you can see i've commented some of them out and tried different ones:
;[mytest]
;driver = MySQL
;Database = moodle
;Server = localhost
;Socket = /var/lib/mysql/mysql.sock
;[mynew]
;Description = MySQL
;Driver = MySQL
;SERVER = localhost
;USER = root
;PASSWORD =
;PORT = 3306
;DATABASE = moodle
[Default]
Driver = /usr/lib/libmyodbc5.so
Description = Connector/ODBC 5 Driver DSN
SERVER = localhost
PORT =
USER = root
Password =
Database = moodle
SOCKET =
However, whenever i run
isql -v
to see if there are any problems, i always get:
[IM010][unixODBC][Driver Manager]Data source name too long
[ISQL]ERROR: Could not SQLConnect
My googling around the error only ever seems to turn up results for people having a connection string within something like ASP, nothing about how to get it working on the server in this way...
Could anyone offer me any advice/help?
If you need more information, let me know.
Thank you!
When launching isql you have to specify the data source name as defined in the man page.
SYNOPSIS
isql DSN [UID [PWD]] [options]
OPTIONS
DSN Name of the data source you want to connect to.
Given your configuration in /etc/odbc.ini, you would launch isql -v Default to test your connection. In the configuration file, the data source name is the one you've defined between brackets.

Categories