PHP Connect to MSSQL From Both Linux and Windows - php

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.

Related

PHP Connection to SQL Server 20005

I have PHP installed on a web server and also have MySQL on the same server and all works perfectly.
I now have SQL Server 2005 installed on another server 192.168.90.250 that I want to connect to as there is a view i want to display in PHP.
I have read around for a number of days but still can not get a connection to work.
I am running on the web server:
Windows Server 2008 R2
Apache 2.2
P 5.3.0
And SQL Server 2005 on another server
I presume I need to download a driver like SQLSRV30 and place the correct .dll file into the C:\Program Files (x86)\PHP\ext directory.
I presume I then need to edit the php.ini file and add something like the following:
extension=php_sqlsrv_53_ts.dll
I would then need to restart the apache server.
At this point I presume I would be able to use something like the following to connect to SQL Server:
<?php
if (function_exists('mssql_connect')) {
die("Your PHP installation does not have MSSQL support.");
}
$db = mssql_connect ($hostname, $username, $password);
if($db){
echo "no connection";
}else{
echo "connected";
}
?>
It seems as if I can not run the function mssql_connect, as the page just displays a blank page.
If I comment out the:
//$db = mssql_connect ($hostname, $username, $password);
the page at least displays some data.
I have literally spent days on this, what am I doing wrong?
Am I using the correct driver as there are different versions?
Thanks for your help.
D
For using the sqlsrv extension you need to use sqlsrv_ functions or better yet switch to PDO

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 :(

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

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);

Php character coding

I have a file named index.php which using a mysql server gets a simple username. The mysql server is running on centOS and I have two different systems running apache serving as web servers.
One is my own windows pc using a "wamp" solution which uses the mysql server refereed before and the other is the centOS server itself.
I use this so I can develop in my laptop and run the final on the centOS box.
The problem is this:
Accessing centOS box I get (on hxxp://centos):
out_sider 1lu�s 2oi
Using wamp on windows I get (on hxxp://localhost):
out_sider 1luís 2oi
The mysql database is configured correctly seeing that both use the same and I used svn repository to move files from windows to centOS so the file is the same.
Does anyone have any suggestions?
Thanks in advnce
My first guess is its probably a locale thing, try adding or updating locale settings of the centOS, or try controlling it from PHP.
str = iconv(mb_detect_encoding(str)/*or insert the encoding type if you know it*/, "UTF-8", str);
and try also:
str = mb_convert_encoding (str, "UTF-8");
Also try connecting and querying the MYSQL server directly, to rule out the fact that it maybe the MYSQL table charset (on centOS) or something.

Categories